I have two matrices user_vecs and item_vecs

I am trying to take the dot product of the two to build a recommendation engine:

The shape of the two vectors are as follows:


(20051, 20)



When i take the dot product of the transpose as follows:

a = user_vecs.dot(item_vecs.transpose())

I get the following error:

ValueError                                Traceback (most recent call last)
<ipython-input-41-f4cd01978711> in <module>
----> 1 a = user_vecs.dot(item_vecs.transpose())

C:\ProgramData\Anaconda3\lib\site-packages\scipy\sparse\base.py in dot(self, other)
    363         """
--> 364         return self * other
    366     def power(self, n, dtype=None):

C:\ProgramData\Anaconda3\lib\site-packages\scipy\sparse\base.py in __mul__(self, other)
    479         if issparse(other):
    480             if self.shape[1] != other.shape[0]:
--> 481                 raise ValueError('dimension mismatch')
    482             return self._mul_sparse_matrix(other)

ValueError: dimension mismatch

i understand that the dimensions of the two matrices are not matching, but the transpose should have fixed that. Why am I still getting this error?


Firstly user_vecs, item_vecs are matrices, not vectors, since both the shapes are of two dimensions. If seems like you are trying to multiply both the matrices, so you can simply use user_vecs.dot(item_vecs). No need to transpose item_vecs. You can also use np.matmul(user_vecs, item_vecs). Refer to this thread for more details.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.