(row)
transform data into sparse format
transform data into sparse format
transform data into sparse format
def data_format_transform(row):

        """
        transform data into sparse format
        """

        if type(row.features).__name__ != consts.SPARSE_VECTOR:
            feature_shape = row.features.shape[0]
            indices = []
            data = []

            for i in range(feature_shape):
                if np.isnan(row.features[i]):
                    indices.append(i)
                    data.append(NoneType())
                elif np.abs(row.features[i]) < consts.FLOAT_ZERO:
                    continue
                else:
                    indices.append(i)
                    data.append(row.features[i])

            new_row = copy.deepcopy(row)
            new_row.features = SparseVector(indices, data, feature_shape)
            return new_row
        else:
            sparse_vec = row.features.get_sparse_vector()
            replace_key = []
            for key in sparse_vec:
                if sparse_vec.get(key) == NoneType() or np.isnan(sparse_vec.get(key)):
                    replace_key.append(key)

            if len(replace_key) == 0:
                return row
            else:
                new_row = copy.deepcopy(row)
                new_sparse_vec = new_row.features.get_sparse_vector()
                for key in replace_key:
                    new_sparse_vec[key] = NoneType()
                return new_row
def data_format_transform ( row ) : if type ( row . features ) . __name__ != consts . SPARSE_VECTOR : feature_shape = row . features . shape [ 0 ] indices = [ ] data = [ ] for i in range ( feature_shape ) : if np . isnan ( row . features [ i ] ) : indices . append ( i ) data . append ( NoneType ( ) ) elif np . abs ( row . features [ i ] ) < consts . FLOAT_ZERO : continue else : indices . append ( i ) data . append ( row . features [ i ] ) new_row = copy . deepcopy ( row ) new_row . features = SparseVector ( indices , data , feature_shape ) return new_row else : sparse_vec = row . features . get_sparse_vector ( ) replace_key = [ ] for key in sparse_vec : if sparse_vec . get ( key ) == NoneType ( ) or np . isnan ( sparse_vec . get ( key ) ) : replace_key . append ( key ) if len ( replace_key ) == 0 : return row else : new_row = copy . deepcopy ( row ) new_sparse_vec = new_row . features . get_sparse_vector ( ) for key in replace_key : new_sparse_vec [ key ] = NoneType ( ) return new_row