Skip to content

Commit 2e0f093

Browse files
committed
statistics: Speed up countnans for sparse matrices
1 parent 468e3fa commit 2e0f093

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Orange/statistics/util.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ def _count_nans_per_row_sparse(X, weights, dtype=None):
2525

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

29-
return np.fromiter((np.sum(row.data) for row in w), dtype=dtype)
30-
31-
return np.fromiter((np.isnan(row.data).sum() for row in X), dtype=dtype)
30+
if isinstance(X, (sp.csr_matrix, sp.csc_matrix)):
31+
X = type(X)((np.isnan(X.data), X.indices, X.indptr), X.shape)
32+
return np.asarray(X.sum(axis=1), dtype=dtype).ravel()
33+
else: # pragma: no cover
34+
return np.fromiter((np.isnan(row.data).sum() for row in X), dtype=dtype)
3235

3336

3437
def sparse_count_implicit_zeros(x):

0 commit comments

Comments
 (0)