minimum of a sparse matrix?

numpy, scipy, sparse-matrix

Solution

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 per row or column, you can `map` this over `X.getrow(i) for i in X.shape[0]` or `X.shape[1]`.

But you're right, this should be a method.

Problem

There does not seem to be a method in scipy.sparse which gives the minimum of a sparse matrix. In particular, I seek the minimum of the columns. No method appears in the docs and numpy minimum does not apply. If `X` is a sparse matrix, `X.min()` also throws the error: `*** AttributeError: 'module' object has no attribute 'min'`. Surely this must be something people use. How is this done?

Original source