7

如果你有一个稀疏矩阵 X:

>> X = csr_matrix([[0,2,0,2],[0,2,0,1]])
>> print type(X)    
>> print X.todense()    
<class 'scipy.sparse.csr.csr_matrix'>
[[0 2 0 2]
 [0 2 0 1]]

和一个矩阵 Y:

>> print type(Y)
>> print text_scores
<class 'numpy.matrixlib.defmatrix.matrix'>
[[8]
 [5]]

...如何将 X 的每个元素乘以 Y 的行。例如:

[[0*8 2*8 0*8 2*8]
 [0*5 2*5 0*5 1*5]]

或者:

[[0 16 0 16]
 [0 10 0 5]]

我已经厌倦了这个,但显然它不起作用,因为尺寸不匹配: Z = X.data * Y

4

3 回答 3

9

不幸的是,.multiply如果另一个矩阵是密集的,则 CSR 矩阵的方法似乎会使矩阵密集。所以这将是避免这种情况的一种方法:

# Assuming that Y is 1D, might need to do Y = Y.A.ravel() or such...

# just to make the point that this works only with CSR:
if not isinstance(X, scipy.sparse.csr_matrix):
    raise ValueError('Matrix must be CSR.')

Z = X.copy()
# simply repeat each value in Y by the number of nnz elements in each row: 
Z.data *= Y.repeat(np.diff(Z.indptr))

这确实会创建一些临时变量,但至少是完全矢量化的,并且不会使稀疏矩阵变密。


对于 COO 矩阵,等价于:

Z.data *= Y[Z.row] # you can use np.take which is faster then indexing.

对于 CSC 矩阵,等效值为:

Z.data *= Y[Z.indices]
于 2012-09-02T17:22:24.450 回答
1

我用来执行逐行(或逐列)乘法的方法是使用矩阵乘法,左侧有对角矩阵(右侧):

import numpy as np
import scipy.sparse as sp

X = sp.csr_matrix([[0,2,0,2],
                   [0,2,0,1]])
Y = np.array([8, 5])

D = sp.diags(Y) # produces a diagonal matrix which entries are the values of Y
Z = D.dot(X) # performs D @ X, multiplication on the left for row-wise action

保留稀疏性(以 CSR 格式):

print(type(Z))
>>> <class 'scipy.sparse.csr.csr_matrix'>

并且输出也是正确的:

print(Z.toarray()) # Z is still sparse and gives the right output
>>> print(Z.toarray()) # Z is still sparse and gives the right output
[[ 0. 16.  0. 16.]
 [ 0. 10.  0.  5.]]
于 2020-08-22T17:01:42.147 回答
0

我有同样的问题。就我个人而言,我没有找到scipy.sparse非常有用的文档,也没有找到直接处理它的函数。所以我试着自己写,这为我解决了:

Z = X.copy()
for row_y_idx in range(Y.shape[0]):
    Z.data[Z.indptr[row_y_idx]:Z.indptr[row_y_idx+1]] *= Y[row_y_idx, 0]

Y这个想法是:对于第 -th位置的每个元素row_y_idx,执行与row_y_idx第 -th 行的标量乘法X有关在此处访问 CSR 矩阵中的元素的更多信息(其中datais AIAis indptr)。

给定XY按照您的定义:

import numpy as np
import scipy.sparse as sps

X = sps.csr_matrix([[0,2,0,2],[0,2,0,1]])
Y = np.matrix([[8], [5]])

Z = X.copy()
for row_y_idx in range(Y.shape[0]):
    Z.data[Z.indptr[row_y_idx]:Z.indptr[row_y_idx+1]] *= Y[row_y_idx, 0]

print(type(Z))
print(Z.todense())

输出与您的相同:

<class 'scipy.sparse.csr.csr_matrix'>
 [[ 0 16  0 16]
  [ 0 10  0  5]]
于 2019-01-05T17:37:23.353 回答