How do I raise a scipy.sparse
matrix to a power, element-wise? numpy.power
should, according to its manual, do this, but it fails on sparse matrices:
>>> 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
Same problem with X**2
. Converting to a dense array works, but wastes precious seconds.
I've had the same problem with np.multiply
, which I solved using the sparse matrix's multiply
method, but there seems to be no pow
method.
__pow__
method, which is attempting to square the entire matrix, rather than doing it elementwise. That fails because, as it says, the matrix isn't square. – Starla(1,)
or(1,1)
array for the purposes ofnumpy.power
on a dense array. – Heraclid