Slicing Sparse Matrices in Scipy — Which Types Work Best?

ⅰ亾dé卋堺 提交于 2019-11-27 19:22:36

Ok, so I'm pretty sure the "right" way to do this is: if you are slicing columns, use tocsc() and slice using a list/array of integers. Boolean vectors does not seem to do the trick with sparse matrices -- the way it does with ndarrays in numpy. Which means the answer is.

indices = np.where(bool_vect)[0]
out1 = M.tocsc()[:,indices]
out2 = M.tocsr()[indices,:]

But question: is this the best way? Is this in place?

In practice this does seem to be happening in place -- and it is much faster than prior attempts (using lil_matrix).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!