With CSR / CSC matrices use
def min_sparse(X): if len(X.data) == 0: return 0 m = X.data.min() return m if X.getnnz() == X.size else min(m, 0)
To do this for each row or column, you can map do this via X.getrow(i) for i in X.shape[0] or X.shape[1] .
But you are right, it must be a method.
source share