(X, weights, dtype=None)
Count the number of nans (undefined) values per row.
Count the number of nans (undefined) values per row.
Count the number of nans ( undefined ) values per row .
def _count_nans_per_row_sparse(X, weights, dtype=None):
    """ Count the number of nans (undefined) values per row. """
    if weights is not None:
        X = X.tocoo(copy=False)
        nonzero_mask = np.isnan(X.data)
        nan_rows, nan_cols = X.row[nonzero_mask], X.col[nonzero_mask]

        if weights.ndim == 1:
            data_weights = weights[nan_rows]
        else:
            data_weights = weights[nan_rows, nan_cols]

        w = sp.coo_matrix((data_weights, (nan_rows, nan_cols)), shape=X.shape)
        w = w.tocsr()
        return np.asarray(w.sum(axis=1), dtype=dtype).ravel()

    if isinstance(X, (sp.csr_matrix, sp.csc_matrix)):
        X = type(X)((np.isnan(X.data), X.indices, X.indptr), X.shape)
        return np.asarray(X.sum(axis=1), dtype=dtype).ravel()
    else:  # pragma: no cover
        raise TypeError("unsupported type '{}'".format(type(X).__name__))
def _count_nans_per_row_sparse ( X , weights , dtype = None ) : if weights is not None : X = X . tocoo ( copy = False ) nonzero_mask = np . isnan ( X . data ) nan_rows , nan_cols = X . row [ nonzero_mask ] , X . col [ nonzero_mask ] if weights . ndim == 1 : data_weights = weights [ nan_rows ] else : data_weights = weights [ nan_rows , nan_cols ] w = sp . coo_matrix ( ( data_weights , ( nan_rows , nan_cols ) ) , shape = X . shape ) w = w . tocsr ( ) return np . asarray ( w . sum ( axis = 1 ) , dtype = dtype ) . ravel ( ) if isinstance ( X , ( sp . csr_matrix , sp . csc_matrix ) ) : X = type ( X ) ( ( np . isnan ( X . data ) , X . indices , X . indptr ) , X . shape ) return np . asarray ( X . sum ( axis = 1 ) , dtype = dtype ) . ravel ( ) else : # pragma: no cover raise TypeError ( "unsupported type '{}'" . format ( type ( X ) . __name__ ) )