Element-wise power of scipy.sparse matrix?

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

Up vote 5 down vote favorite 1 share g+ share fb share tw.

How to 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 ' with 144875 stored elements in Compressed Sparse Row format> >>> np. Power(X, 2) Traceback (most recent call last): File "", line 1, in 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.

Python numpy scipy sparse-matrix exponentiation link|improve this question asked Jun 21 '11 at 20:27larsmans71.4k760133 92% accept rate.

I'm not familiar enough with numpy to tell you the answer, but your code doesn't contradict the documentation. The second argument to numpy. Power should not be a number, but another matrix.

– RoundTower Jun 21 '11 at 20:46 It looks like it's calling the object's __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. – Thomas K Jun 21 '11 at 21:05 @RoundTower: actually, the second element should be an array, which it not the same as a matrix in Numpy, but a scalar is the same as a (1,) or (1,1) array for the purposes of numpy.

Power on a dense array. – larsmans Jun 21 '11 at 21:30.

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 cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions