Cauchy Schwarz
Cauchy Schwarz

Reputation: 827

How does numpy.dot function work?

import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([5, 6])
a.dot(b)
b.dot(a)

What happens for a.dot(b) and b.dot(a)? For matrix mutiplication, a.dot(b) should be illegal.

Upvotes: 5

Views: 3163

Answers (1)

fuglede
fuglede

Reputation: 18201

In this setup, b.dot(a) is equivalent to b.T.dot(a); indeed, b and b.T happen to have the same shape, so even though the notation makes it looks like b is a row vector, it really isn't. We can, however, redefine it to behave explicitly as a row vector, in which case the operation fails as expected:

In [25]: b.dot(a)
Out[25]: array([23, 34])

In [26]: b.T.dot(a)
Out[26]: array([23, 34])

In [30]: b.shape
Out[30]: (2,)

In [31]: b.T.shape
Out[31]: (2,)

In [27]: c = b.reshape((1, 2))

In [28]: c.dot(a)
Out[28]: array([[23, 34]])

In [29]: a.dot(c)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-29-5d91888f8066> in <module>()
----> 1 a.dot(c)

ValueError: shapes (2,2) and (1,2) not aligned: 2 (dim 1) != 1 (dim 0)

Upvotes: 4

Related Questions