Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dot routine for scipy.sparse matrices produces error

I have a CSR matrix:

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

I want to take dot 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. So I 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 are the rows of the same matrix, so their dimentions ought to match:

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

Why does dot subroutine not work?

Thanks.

like image 596
abhinavkulkarni Avatar asked Aug 08 '13 23:08

abhinavkulkarni


1 Answers

To perform a dot product of two row vectors, you have to transpose one. the one to transpose depends on the result you're 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]])

Versus

In [14]: a.transpose().dot(b)
Out[14]: 
matrix([[ 4,  5,  6],
        [ 8, 10, 12],
        [12, 15, 18]])
like image 153
Justin Avatar answered Oct 10 '22 05:10

Justin