python - dot routine for scipy.sparse matrices produces error -


i have csr matrix:

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

i want take dot product of 2 rows of csr matrix:

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

both v1 , v2 csr matrices. use dot subroutine:

>> 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 

they rows of same matrix, dimentions ought match:

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

why dot subroutine not work?

thanks.

to perform dot product of 2 row vectors, have transpose one. 1 transpose depends on result you're looking for.

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

versus

in [14]: a.transpose().dot(b) out[14]:  matrix([[ 4,  5,  6],         [ 8, 10, 12],         [12, 15, 18]]) 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -