# Dimension Mismatch Error during dot product in Python

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:

user_vecs.shape
(20051, 20)

item_vecs.shape
(20,1808)


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)
362
363         """
--> 364         return self * other
365
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)
483

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 and item_vecs are matrices, not vectors, since both the shapes are of two dimensions. It 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.
Mathematically speaking this is matrix dot product. i.e. the number of columns in user_vecs must match the number of rows (lines) in item_vecs. In this case the matching is already established since performing a dot product on (20051, 20) by (20,1808) is mathematically feasible. You need to just to put on the product directly.
a = user_vecs.dot(item_vecs)