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

Pauli Virtanen pav@iki...
Sun Sep 11 13:23:45 CDT 2011


Sun, 11 Sep 2011 03:03:26 -0500, Pengkui Luo wrote:
[clip]
> 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.

	Pauli



More information about the NumPy-Discussion mailing list