[Numpy-discussion] arrray/matrix nonzero() type

Nathan Bell wnbell@gmail....
Wed May 27 16:47:21 CDT 2009


On Wed, May 27, 2009 at 3:26 PM, Nicolas Rougier
<Nicolas.Rougier@loria.fr> wrote:
>
> Hi again,
>
> I  have a problem with the nonzero() function for matrix.
>
> The following test program:
>
> import numpy, scipy.sparse
>
> Z = numpy.zeros((10,10))
>
> i = Z.nonzero()
> print i
> Zc = scipy.sparse.coo_matrix((Z[i],i))
>
> Z = numpy.matrix(Z)
> i = Z.nonzero()
> print i
> Zc = scipy.sparse.coo_matrix((Z[i],i))
>
>
> Is that the intended behavior ? How can I use nonzero with matrix to
> build the coo one ?
>

Even simpler, just do
Zc = scipy.sparse.coo_matrix(Z)

As of SciPy 0.7, all the sparse matrix constructors accept dense
matrices and array-like objects.

The problem with the matrix case is that Z[i] is rank-2 when a rank-1
array is expected.

-- 
Nathan Bell wnbell@gmail.com
http://graphics.cs.uiuc.edu/~wnbell/


More information about the Numpy-discussion mailing list