+2 votes

Best answer

The * nonzero() *function of the

You can combine the row and column arrays returned by the * nonzero() *function using

Here is an example to show how to use the *nonzero()* function:

>>> import numpy as np

>>> from scipy.sparse import csr_matrix, save_npz, load_npz

>>> r = np.array([0, 0, 1, 2, 2, 3, 3, 3])

>>> c = np.array([0, 1, 2, 1, 2, 0, 2, 3])

>>> v = np.array([1, 2, 3, 4, 5, 6, 7, 8])

>>> x=csr_matrix((v, (r,c)), shape=(4,4))

>>> x

<4x4 sparse matrix of type '<class 'numpy.int64'>'

with 8 stored elements in Compressed Sparse Row format>

>>> x.toarray()

array([[1, 2, 0, 0],

[0, 0, 3, 0],

[0, 4, 5, 0],

[6, 0, 7, 8]])

>>> i,j=x.nonzero()>>> i

array([0, 0, 1, 2, 2, 3, 3, 3], dtype=int32)

>>> j

array([0, 1, 2, 1, 2, 0, 2, 3], dtype=int32)

>>> list(zip(i,j))[(0, 0), (0, 1), (1, 2), (2, 1), (2, 2), (3, 0), (3, 2), (3, 3)]