How to find the indices of columns that are not entirely zeros of a sparse matrix

Question:

I have a large sparse array (Python csr). How can I find the indices of columns that are not entirely zeros?

For example, if the matrix looks like s constructed below

In [13]: import  scipy.sparse as sparse

In [14]: s=sparse.dok_matrix((2,4))

In [15]: s[0,0]=8; s[0,3]=9

In [16]:  print (s.toarray())
[[8. 0. 0. 9.]
 [0. 0. 0. 0.]]

The nonzero indices for the matrix s will be [0,3].

Asked By: zell

||

Answers:

I think you can use:

import numpy as np
np.nonzero((s!=0).sum(0))[1]

output: [0, 3]

Answered By: mozway
from scipy.sparse import csr_matrix                                                                     
                                                                                                        
A = csr_matrix([[1,2,0],[0,0,3],[4,0,5]])                                                               
                                                                                                        
nonzero_indices = A.nonzero()                                                                           

nonzero() will return a tuple of two lists containing the indices you’re looking for. For example:

for i,_ in enumerate(nonzero_indices[0]):                                                               
    print(nonzero_indices[0][i], nonzero_indices[1][i])   

will give

0 0
0 1
1 2
2 0
2 2
Answered By: JustLearning
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.