[Numpy-discussion] How to apply Numpy ufunc to Scipy sparse matrices?

Pauli Virtanen pav at iki.fi
Sun Sep 11 14:23:45 EDT 2011

Sun, 11 Sep 2011 03:03:26 -0500, Pengkui Luo wrote:
> However, converting a large sparse matrix to dense would easily eat up
> the memory. Is there a way for np.sign (as well as other ufunc) to take
> a sparse matrix as parameter, and return a sparse matrix?

For CSR, CSC, and DIA you can do

	x.data = some_ufunc(x.data)

to operate on the nonzero entries. If some_ufunc(0) == 0, that's all you
need. Otherwise, the matrix becomes dense.


More information about the NumPy-Discussion mailing list