[SciPy-Dev] Bug in use of np.sign() function with sparse csc_matrix?

Nathaniel Smith njs at pobox.com
Sun Jan 22 13:51:54 EST 2012


Numpy doesn't know anything about scipy sparse matrices in general -
they're just another random user defined python object. Generally you have
to call todense() before calling any numpy function on them.

It wouldn't be that hard for you to implement sign() for csc or csr
matrices directly, since the pattern of nnz's doesn't change - just make a
new matrix that has the same indices and indptr arrays, but call sign() on
the data array.

- Nathaniel
On Jan 22, 2012 9:52 AM, "Warren Weckesser" <warren.weckesser at enthought.com>
wrote:

> In all the examples that I've tried with a sparse csc_matrix `a`,
> `sign(a)` always returns 1.  I expect it to return the matrix of
> element-wise signs of a.  For example:
>
> In [1]: from scipy.sparse import csc_matrix
>
> In [2]: a = csc_matrix([[0.0, 1.0, 2.0], [0.0, 0.0, -3.0], [0.0, 0.0,
> 0.0]])
>
> In [3]: a.todense()
> Out[3]:
> matrix([[ 0.,  1.,  2.],
>         [ 0.,  0., -3.],
>         [ 0.,  0.,  0.]])
>
> In [4]: np.sign(a.todense())
> Out[4]:
> matrix([[ 0.,  1.,  1.],
>         [ 0.,  0., -1.],
>         [ 0.,  0.,  0.]])
>
> In [5]: np.sign(a)   # Incorrect result?
> Out[5]: 1
>
> In [6]: import scipy
>
> In [7]: scipy.__version__
> Out[7]: '0.11.0.dev-81dc505'
>
> In [8]: np.__version__
> Out[8]: '1.6.1'
>
>
> I think that's a bug, but if someone knows better, let me know!
>
> Warren
>
>
> _______________________________________________
> SciPy-Dev mailing list
> SciPy-Dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scipy-dev/attachments/20120122/33237f57/attachment.html>


More information about the SciPy-Dev mailing list