我需要乘两个不同大小的稀疏矩阵(元素)。以下是矩阵:
matrix1 = (1, 2) 30.0
(2, 3) 20.0
(4, 5) 10.0
(6, 7) 80
matrix2 = (1, 2) 2.0
(2, 3) 1.0
(4, 5) 5.0如您所见,matrix1比matrix2大。我需要将它们相乘,使matrix2中不存在的元素(在本例中是元素(6, 7) )保持不变。我需要的输出如下:
final_matrix = (1, 2) 60.0
(2, 3) 20.0
(4, 5) 50.0
(6, 7) 80对于我正在处理的实际数据来说,矩阵是非常大的。如果您需要进一步的澄清,请告诉我。
谢谢!
发布于 2016-05-18 00:10:24
对于密集数组来说,执行这种乘法比较容易。而且很快,因为切片是快速的
In [453]: x=np.arange(24).reshape(4,6)
In [454]: y=np.arange(10,22).reshape(3,4)
In [457]: x[:3,:4] *= y
In [458]: x
Out[458]:
array([[ 0, 11, 24, 39, 4, 5],
[ 84, 105, 128, 153, 10, 11],
[216, 247, 280, 315, 16, 17],
[ 18, 19, 20, 21, 22, 23]])与稀疏等价
In [460]: xM=sparse.csr_matrix(x)
In [462]: yM=sparse.csr_matrix(y)切片乘法工作:
In [468]: z= xM[:3,:4].multiply(yM) # z.A matches the dense block但是,当我们试图将该值重新赋值给xM时,会收到警告。
In [469]: xM[:3,:4] = xM[:3,:4].multiply(yM)
/usr/lib/python3/dist-packages/scipy/sparse/compressed.py:730: SparseEfficiencyWarning: Changing the sparsity structure of a csr_matrix is expensive. lil_matrix is more efficient.
SparseEfficiencyWarning)
In [471]: xL=sparse.lil_matrix(x)
In [472]: yL=sparse.lil_matrix(y)
In [475]: xL[:3,:4]=xL[:3,:4].multiply(yL)xL.multiply代码实际上是:return self.tocsr().multiply(other)
我不知道哪种格式组合最有效。
[我们可以通过认识到xM[:3,:4].multiply(yM)将有更少而不是更多的非零元素来避开csr稀疏警告。因此,至少暂时我们可以将xM.data的一些值设置为0,而不需要更改其他属性。我们可以稍后使用eliminate_zeros进行清理。]
切片赋值的另一种替代方法是将y扩展到x大小,并执行完全乘法。在这种情况下,我们需要用1来填充y。
稠密的版本如下:
In [478]: z=np.ones_like(x)
In [479]: z[:3,:4]=y
In [480]: x*z对于稀疏矩阵,我们必须查看所需的填充。如果yM只比xM小几行和/或列,我怀疑我们可以有效地使用sparse.vstack和hstack。如果有很多填充,结果会有很多非零值,所以我们也可以做密集的z。
In [503]: zM = sparse.vstack((yM,np.ones((1,4),int)))
In [504]: zM = sparse.hstack((zM,np.ones((4,2),int)))
In [505]: zM.shape
Out[505]: (4, 6)
In [507]: zM.A
Out[507]:
array([[10, 11, 12, 13, 1, 1],
[14, 15, 16, 17, 1, 1],
[18, 19, 20, 21, 1, 1],
[ 1, 1, 1, 1, 1, 1]], dtype=int32)
In [511]: xM.multiply(zM).A
Out[511]:
array([[ 0, 11, 24, 39, 4, 5],
[ 84, 105, 128, 153, 10, 11],
[216, 247, 280, 315, 16, 17],
[ 18, 19, 20, 21, 22, 23]], dtype=int32)另一种构建扩展yM的方法是使用sparse.bmat,它从块生成一个新的矩阵。bmat的工作方式是构造coo格式矩阵,并将它们的所有row, col, data属性连接起来,并由此生成一个新的矩阵。
事实证明,vstack使用bmat
return bmat([[b] for b in blocks], format=format, dtype=dtype)这将构造相同的4x6矩阵:
In [520]: zM = sparse.bmat([[yM, np.ones((3,2),int)],
[np.ones((1,4),int), np.ones((1,2),int)]])=================
另一种可能是从dok问题中调整@Vadim的sum方法
https://stackoverflow.com/a/37241977/901925
它是迭代的,并且高度依赖于较小的矩阵的非零元素的数量,但它是相当灵活的。
发布于 2016-05-17 16:53:19
也许有一种更复杂的方法,但从数学上讲,这是一种非常简单的方法。
from scipy.sparse import csc_matrix, lil_matrix, find
## create example matrices, A, B, assume B has values that are not in A
A = csc_matrix( (5,5) )
A[1,1] = 3.0
A[2,2] = 2.0
B = csc_matrix( (5,5) )
B[1,1] = 5.0
B[2,2] = 10.0
B[3,3] = 50.0
C = lil_matrix( (5,5) ) ## C will be a modification of A;
## more efficient to do this with lil_matrix than csc_matrix;
## you can convert later if needed
(I,J,V) = find(A) ## get nonzero indices of A
(M,N,P) = find(B) ## get nonzero indices of B
C[M,N] = 1.0 ## set all B-corresponding elements to 1.0
C[I,J] = A[I,J] ## overwrite with corresponding elements in A
D = C.multiply(B) ## EDIT: per hpaulj's suggestion, using multiply(), which works with most any sparse matrix typehttps://stackoverflow.com/questions/37280871
复制相似问题