如何在scipy.sparse
元素方面将矩阵提升到幂?numpy.power
应该,根据它的手册,这样做,但它在稀疏矩阵上失败:
>>> X
<1353x32100 sparse matrix of type '<type 'numpy.float64'>'
with 144875 stored elements in Compressed Sparse Row format>
>>> np.power(X, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../scipy/sparse/base.py", line 347, in __pow__
raise TypeError('matrix is not square')
TypeError: matrix is not square
同样的问题X**2
。转换为密集阵列是可行的,但会浪费宝贵的时间。
我遇到了同样的问题np.multiply
,我使用稀疏矩阵的multiply
方法解决了这个问题,但似乎没有pow
方法。