如果您有一个稀疏矩阵 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
(说明:该代码段涉及矩阵计算,在IT技术中常见)
Z.data *= Y[Z.row]
,或者如果你关心速度的话,可以使用np.take而不是索引。 - seberg