How dot products NumPy arrays

In this post, we are going to learn about How dot products NumPy arrays. We are going to learn this with the help of many examples and we will see code examples as well. By the end of this post, you will be able to answer the below questions.

  • How to do dot product in numpy.
  • How to multiply matrices in numpy.
  • How to multiply matrix in numpy.
  • How to do matrix multiplication in numpy.

Note: Dot product can be done for two arrays if the number of columns of 1 matrix is equal to the number of rows of the other matrix. If this is not the case then we can not do the dot product of the given arrays. For example:

  • If we have one arrays with size a x N where a is number of rows and N is number of columns.
  • Second array with size N x b where N is number of rows and b is number of columns.
  • The dot product will result in to an array of a x b size.

Numpy dot function


In Python Numpy library, we have dot() function that is used to do the dot product of two arrays. The syntax of numpy.dot() function is:

Syntax

numpy.dot(a, b, out=None)

Conditional results applicable of dot product:

dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
  • If both a and b are 1-D arrays, it is inner product of vectors (without complex conjugation).
  • If both a and b are 2-D arrays, it is matrix multiplication, but using matmul or a @ b is preferred.
  • If either a or b is 0-D (scalar), it is equivalent to multiply and using numpy.multiply(a, b) or a * b is preferred.
  • If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b.
  • If a is an N-D array and b is an M-D array (where M>=2), it is a sum product over the last axis of a and the second-to-last axis of b:

Parameters

ParameterMandatory or NotDetails
a Mandatory First Input arrays to be multiplied.
b Mandatory Second Input arrays to be multiplied
outnot Mandatory out: [ndarray, optional] This must have the exact kind that would be returned if it was not used. In particular, it must have the right type,
must be C-contiguous, and its dtype must be the dtype that would be returned for dot(a,b). This is a performance feature. Therefore,
if these conditions are not met, an exception is raised, instead of attempting to be flexible.

Error:

If the last dimension of a is not the same size as the second-to-last dimension of b.

1. NumPy dot with 2 Scaler numbers


In this example, we are just doing the dot product of a scaler number with another scaler number which will work as simple multiplication of two numbers.

import numpy as np
num1 = 5
num2 = 4
   
product = np.dot(num1, num2) 
print ("Multiplication Result is : ", product) 

Output

Multiplication Result is : 20

You May also like


2. NumPy dot product of Scaler numbers with linear array


In this example, we are just doing the dot product of a scaler number with a 1-D linear array.Which will work as a Simple multiplication of the scaler number with each element of the scaler array. This condition will come into play:If either a or b is 0-D (scalar), it is equivalent to multiplying and using numpy.multiply(a, b) or a * b is preferred.

import numpy as np
num1 = 5
arr1 = np.array([1, 2, 3] )
   
product = np.dot(num1, arr1) 
print ("Multiplication Result is : ", product) 

Output

Multiplication Result is : [5,10,15]

3.NumPy Program dot product of 1D arrays


In this example, we are going learn how to multiply two 1D arrays in numpy.It is working like (1×5) + (2×4) + (3×3) + (4×2) + (5×1) = 35

import numpy as np
arr1 = np.array([1, 2, 3, 4, 5] )
arr2 = np.array([5, 4, 3, 2, 1] )
   
print ("1st Input array : ", arr1)
print ("2nd Input array : ", arr2)
   
product = np.dot(arr1, arr2) 
print ("Dot product Result of Linear arrays is : ", product) 

Output

1st Input array :  [1 2 3 4 5]
2nd Input array :  [5 4 3 2 1]
Dot product Result of Linear arrays is :  35

4.NumPy dot to product multi-dimensional Array


In this example, we are going to learn how to multiply two Multiply multi-dimensional arrays in numpy.But the sizes are not suitable for the dot product so we will get a value error.

  
import numpy as np
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([[6, 5, 4], [3, 2, 1]])
   
print ("1st Input array : ", arr1)
print ("2nd Input array : ", arr2)
   
product = np.dot(arr1, arr2) 
print ("Dot product Result of 2D arrays is : ", product) 

Output

1st Input array :  [[1 2 3]
 [4 5 6]]
2nd Input array :  [[6 5 4]
 [3 2 1]]
Traceback (most recent call last):
  File "C:/Users/Contract/Desktop/Programs/NumpyTest.py", line 8, in <module>
    product = np.dot(arr1, arr2)
  File "<__array_function__ internals>", line 5, in dot
ValueError: shapes (2,3) and (2,3) not aligned: 3 (dim 1) != 2 (dim 0)

To resolve this error, we need to make sure the sizes of the arrays are a x N and N x b.Here is a good example:

import numpy as np
arr1 = np.array([[1, 2, 3], [4, 5, 6],[7, 8, 9]])
arr2 = np.array([[9, 8, 7],[6, 5, 4], [3, 2, 1]])
   
print ("1st Input array : ", arr1)
print ("2nd Input array : ", arr2)
   
product = np.dot(arr1, arr2) 
print ("Dot product Result of 2D arrays is : ", product)

Output:

1st Input array :  [[1 2 3]
 [4 5 6]
 [7 8 9]]
2nd Input array :  [[9 8 7]
 [6 5 4]
 [3 2 1]]
Dot product Result of 2D arrays is :  [[ 30  24  18]
 [ 84  69  54]
 [138 114  90]]

An explanation of this dot product is:

0th row of array 1 [1 2 3] and 0th column of array 2
[9 6 3]

will make the first element of the resultant array.

(1x9) + (2x6) + (3x3) = 30

Similarly, row 0 and column 1 will give you the next element.

(1x8) + (2x5) + (3x2) = 24