Numpy dot() function with examples

In this tutorial, we will learn how to use Numpy’s dot() function with multiple examples. Numpy’s dot() function is for fining the dot product of two arrays.

In linear algebra, dot product basically takes two equal-length arrays (vectors) of numbers and returns a single number. For example, if we have two vectors a and b

a = (x1,y1,z1) 

and

b = (x2,y2, z2)

Then the dot product of two vectors is result from

a.b = sum(x1*x2 +y1*y2 +z1*z2) 

A dot product is also called the inner product.

Numpy dot(): Dot product of 1-D arrays with Numpy dot() function

Let us see samples of using Numpy’s dot() function under multiple scenarios. First let us load Numpy.

import numpy as np

In Numpy, dot() function can take two array like objects as input. When the two arguments a and b are 1-D arrays, we get inner product of vectors.

a = np.arange(3)
a

array([0, 1, 2])
b = np.arange(3,6)
b

array([3, 4, 5])

We can see that the dot product of two 1-D arrays gives us the sum of element-wise multiplication.

np.dot(a,b)

14

Numpy dot(): Dot product of 2-D arrays is Matrix Multiplication

When the two input arrays are 2-D arrays, Numpy’s dot() function performs matrix multiplication if they are compatible.

Let us consider two 2×2 arrays and use dot() function to multiply the two 2-D arrays. And then verify the results with other matrix multiplication functions in Numpy.

a = np.arange(4).reshape(2,2)
a

array([[0, 1],
       [2, 3]])
b = np.arange(4,8).reshape(2,2)
b

array([[4, 5],
       [6, 7]])

Let us perform dot product using Numpy’s dot() function. As these input vectors are compatible, dot() function will do matrix multiplication and give us 2×2 array as result .

np.dot(a,b)

array([[ 6,  7],
       [26, 31]])

And we can verify the above result is same matrix multiplication using two matrix multiplication methods, Numpy’s matmul() function

np.matmul(a,b)

array([[ 6,  7],
       [26, 31]])

And Python’s @ symbol for matrix multiplication.

a @ b

array([[ 6,  7],
       [26, 31]])

We can see that all the three give the same results. Numpy strongly recommends to use matmul() or @ for 2D array product instead np.dot() function.

Numpy dot(): Dot product of 2-D array with a scalar

If one of the arguments to dot() function is scalar, i.e. a number, then Numpy’s dot() function perform element wise multiplication.

Here is an example of that.

a = np.arange(9).reshape(3,3)

a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
b = 3

And let us use np.dot() on the 2-D matrix and a scalar.

np.dot(a,b)

array([[ 0,  3,  6],
       [ 9, 12, 15],
       [18, 21, 24]])

We can verify the result is same as element wise multiplication by Numppy’s multiply() function

np.multiply(a, b)

array([[ 0,  3,  6],
       [ 9, 12, 15],
       [18, 21, 24]])

and Python * operator for element wise multiplication

a * b

array([[ 0,  3,  6],
       [ 9, 12, 15],
       [18, 21, 24]])

And all the results are same as expected. When either a or b is 0-D (scalar), using numpy.multiply(a, b) or a * b is preferred.