我该如何对一个 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
方法。
__pow__
方法,试图对整个矩阵进行平方运算,而不是逐个元素进行。这会失败,因为正如它所说,该矩阵不是方阵。 - Thomas Knumpy.power
在密集数组上的目的,标量与(1,)
或(1,1)
数组相同。 - Fred Foo