[英]Using broadcasting with sparse scipy matrices
I have a numpy
array Z
with shape (k,N)
and a second array X
with shape (N,n)
. 我有一个形状为(k,N)
的numpy
数组Z
和一个形状为(N,n)
的第二个数组X
Using numpy
broadcasting, I can easily obtain a new array H
with shape (n,k,N)
whose slices are the array Z
whose rows have been multiplied by the columns of X
: 使用numpy
广播,我可以轻松地获得一个新的数组H
,其形状为(n,k,N)
其切片为数组Z
其行已乘以X
的列:
H = Z.reshape((1, k, N)) * X.T.reshape((n, 1, N))
This works fine and is surprisingly fast. 这工作正常,而且速度惊人。 Now, X
is extremely sparse, and I want to further speed up this operation using sparse matrix operations. 现在, X
非常稀疏,我想使用稀疏矩阵运算进一步加速此操作。
However if I perform the following operations: 但是,如果我执行以下操作:
import scipy.sparse as sprs
spX = sprs.csr_matrix(X)
H = (Z.reshape((1,k,N))*spX.T.reshape((n,1,N))).dot(Z.T)
I get the following error: 我收到以下错误:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Python27\lib\site-packages\scipy\sparse\base.py", line 126, in reshape
self.__class__.__name__)
NotImplementedError: Reshaping not implemented for csc_matrix.
Is there a way to use broadcasting with sparse scipy
matrices? 有没有办法使用稀疏scipy
矩阵的广播?
Scipy sparse matrices are limited to 2D shapes. Scipy稀疏矩阵仅限于2D形状。 But you can use Numpy in a "sparse" way: 但是你可以以“稀疏”的方式使用Numpy:
H = np.zeros((n,k,N), np.result_type(Z, X))
I, J = np.nonzero(X)
Z_ = np.broadcast_to(Z, H.shape)
H[J,:,I] = Z_[J,:,I] * X[I,J,None]
Unfortunately the result H
is still a dense array. 不幸的是,结果H
仍然是密集阵列。
Nb indexing with None
is a handy way to add a unit-length dimension at the desired axis. 使用None
Nb索引是在所需轴上添加单位长度尺寸的便捷方法。 The order of the result when combining advanced indexing with slicing is explained in the docs . 将高级索引与切片相结合时的结果顺序在文档中进行了解释 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.