
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)) Z[0,0] = Z[1,1] = 1 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)) gives me: (array([0, 1]), array([0, 1])) (matrix([[0, 1]]), matrix([[0, 1]])) Traceback (most recent call last): File "test.py", line 13, in <module> Zc = scipy.sparse.coo_matrix((Z[i],i)) File "/Volumes/Data/Local/lib/python2.6/site-packages/scipy/sparse/ coo.py", line 179, in __init__ self._check() File "/Volumes/Data/Local/lib/python2.6/site-packages/scipy/sparse/ coo.py", line 194, in _check nnz = self.nnz File "/Volumes/Data/Local/lib/python2.6/site-packages/scipy/sparse/ coo.py", line 187, in getnnz raise ValueError('row, column, and data arrays must have rank 1') ValueError: row, column, and data arrays must have rank 1 Is that the intended behavior ? How can I use nonzero with matrix to build the coo one ? Nicolas

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/
participants (2)
-
Nathan Bell
-
Nicolas Rougier