如果你有一个稀疏矩阵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]]
Run Code Online (Sandbox Code Playgroud)
矩阵Y:
>> print type(Y)
>> print text_scores
<class 'numpy.matrixlib.defmatrix.matrix'>
[[8]
[5]]
Run Code Online (Sandbox Code Playgroud)
...如何将X的每个元素乘以Y的行.例如:
[[0*8 2*8 0*8 2*8]
[0*5 2*5 0*5 1*5]]
Run Code Online (Sandbox Code Playgroud)
要么:
[[0 16 0 16]
[0 10 0 5]]
Run Code Online (Sandbox Code Playgroud)
我已经厌倦了这个,但显然它不起作用,因为尺寸不匹配:
Z = X.data * Y
我想用数组中给定的标量划分稀疏矩阵的行。
例如,我有一个csr_matrix C:
C = [[2,4,6], [5,10,15]]
D = [2,5]
Run Code Online (Sandbox Code Playgroud)
我希望C除法后的结果是:
result = [[1, 2, 3], [1, 2, 3]]
Run Code Online (Sandbox Code Playgroud)
我已经使用我们用于numpy数组的方法尝试了这个:
result = C / D[:,None]
Run Code Online (Sandbox Code Playgroud)
但这似乎真的很慢。如何在稀疏矩阵中有效地做到这一点?