Today we’ll be talking about the dot function from the numpy module which is used to calculate the dot product.
The dot function returns a scalar or an array. It returns a scalar if we pass two scalars as arguments:
>>> print(np.dot(5, 3))
15
It also returns a scalar if we pass two 1-dimensional arrays as arguments:
>>> a = np.array([2, 4, 6])
>>> b = np.array([-1, 2, 3])
>>> print(np.dot(a, b))
24
This is how the dot product is calculated:
For 2-dimensional arrays the dot function returns the result of matrix multiplication. Just a quick reminder: We can only multiply two matrices if the first of them has the same number of columns as the number of rows of the other matrix. So, for example, we can multiply these two matrices:
A = [[4, 2, 3, 8],
[2, 1, 3, 5],
[1, 1, 2, 6]]
B = [[1, 3],
[2, 2],
[8, 2],
[5, 3]]
because A has 4 columns and has 4 rows.
Have a look:
Let’s create nested lists and convert them to arrays:
>>> a = [[4, 2, 3, 8],
... [2, 1, 3, 5],
... [1, 1, 2, 6]]
...
>>> b = [[1, 3],
... [2, 2],
... [8, 2],
... [5, 3]]
...
>>> A = np.array(a)
>>> B = np.array(b)
>>> print(A)
[[4 2 3 8]
[2 1 3 5]
[1 1 2 6]]
>>> print(B)
[[1 3]
[2 2]
[8 2]
[5 3]]
We can only multiply two matrices if the number of columns of the first matrix is equal to the number of rows of the second matrix. Or, more generally, the shape of the last dimension of the first matrix must be the same as the last but one dimension of the second matrix. In our case the two shapes are equal:
>>> A.shape[-1] == B.shape[-2]
True
So we can multiply these two matrices. Let’s use the dot function:
>>> print(np.dot(A, B))
[[72 46]
[53 29]
[49 27]]
If you don’t remember how matrices are multiplied, here’s a visualization of our example:
If the number of columns of the first array is different than the number of rows of the second array, we’ll get an error, because it’s impossible to multiply such two matrices.
Here’s the video version of this article: