The point for scipy.sparse matrices causes an error

I have a CSR matrix :

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

I want to get a point product of two rows of this CSR matrix :

 >> v1 = tfidf.getrow(1) >> v2 = tfidf.getrow(2) >> print type(v1) <class 'scipy.sparse.csr.csr_matrix'> 

Both v1 and v2 are also CSR matrices. Therefore, I use the dot routine:

 >> print v1.dot(v2) Traceback (most recent call last): File "cosine.py", line 10, in <module> print v1.dot(v2) File "/usr/lib/python2.7/dist-packages/scipy/sparse/base.py", line 211, in dot return self * other File "/usr/lib/python2.7/dist-packages/scipy/sparse/base.py", line 246, in __mul__ raise ValueError('dimension mismatch') ValueError: dimension mismatch 

These are rows of the same matrix, therefore their parameters should correspond to:

 >> print v1.shape (1, 4507) >> print v2.shape (1, 4507) 

Why doesn't dot routine work?

Thanks.

+4
source share
1 answer

To execute the point product of two row vectors, you must transfer it. The transposition depends on the result you are looking for.

 import scipy as sp a = sp.matrix([1, 2, 3]) b = sp.matrix([4, 5, 6]) In [13]: a.dot(b.transpose()) Out[13]: matrix([[32]]) 

Vs

 In [14]: a.transpose().dot(b) Out[14]: matrix([[ 4, 5, 6], [ 8, 10, 12], [12, 15, 18]]) 
+6
source

Source: https://habr.com/ru/post/1496063/


All Articles