If you have a sparse matrix 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]]
And a matrix Y:
>> print type(Y)
>> print text_scores
<class 'numpy.matrixlib.defmatrix.matrix'>
[[8]
[5]]
…How can you multiply each element of X by the rows of Y. For example:
[[0*8 2*8 0*8 2*8]
[0*5 2*5 0*5 1*5]]
or:
[[0 16 0 16]
[0 10 0 5]]
I’ve tired this but obviously it doesn’t work as the dimensions dont match:
Z = X.data * Y
Unfortunatly the
.multiplymethod of the CSR matrix seems to densify the matrix if the other one is dense. So this would be one way avoiding that:This does create some temporaries, but at least its fully vectorized, and it does not densify the sparse matrix.
For a COO matrix the equivalent is:
For a CSC matrix the equivalent would be: