Element-wise power of scipy.sparse matrix

五迷三道 提交于 2019-12-01 02:31:08

This is a little low-level, but for element-wise operations you can work with the underlying data array directly:

>>> import scipy.sparse
>>> X = scipy.sparse.rand(1000,1000, density=0.003)
>>> X = scipy.sparse.csr_matrix(X)
>>> Y = X.copy()
>>> Y.data **= 3
>>> 
>>> abs((X.toarray()**3-Y.toarray())).max()
0.0

I just ran into the same question and find that sparse matrix now supports element-wise power. For the case above, it should be:

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