Iterating through a scipy.sparse vector (or matrix)

前端 未结 6 757
你的背包
你的背包 2020-11-30 22:31

I\'m wondering what the best way is to iterate nonzero entries of sparse matrices with scipy.sparse. For example, if I do the following:

from scipy.sparse im         


        
6条回答
  •  自闭症患者
    2020-11-30 23:11

    I had the same problem and actually, if your concern is only speed, the fastest way (more than 1 order of magnitude faster) is to convert the sparse matrix to a dense one (x.todense()), and iterating over the nonzero elements in the dense matrix. (Though, of course, this approach requires a lot more memory)

提交回复
热议问题