Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think you can use:

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

output: [0, 3]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading