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

Warren Weckesser warren.weckesser at enthought.com
Sun Jan 22 14:11:10 EST 2012


On Sun, Jan 22, 2012 at 12:51 PM, Nathaniel Smith <njs at pobox.com> wrote:

> 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.
>

It was while experimenting with an implementation of exactly this idea,
inspired by the pull request https://github.com/scipy/scipy/pull/138, that
I stumbled across this behavior of sign().

Attempting to use, say, np.cos results in an AttributeError.  np.abs works,
because it is implemented in the _data_matrix class (a parent of
csc_matrix):

In [45]: m = csc_matrix([[1.0, -1.25], [1.75, 0.0]])

In [46]: np.cos(m)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/Users/warren/<ipython-input-46-a6e5f2a15c89> in <module>()
----> 1 np.cos(m)

AttributeError: cos

In [47]: np.abs(m)
Out[47]:
<2x2 sparse matrix of type '<type 'numpy.float64'>'
    with 3 stored elements in Compressed Sparse Column format>

In [48]: np.abs(m).todense()
Out[48]:
matrix([[ 1.  ,  1.25],
        [ 1.75,  0.  ]])

In [49]: np.sign(m)
Out[49]: 1

I haven't tracked down why sign(m) returns 1.

Warren



> - 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
>>
>>
> _______________________________________________
> 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/608edcc6/attachment.html>


More information about the SciPy-Dev mailing list