Python numpy.dot() Method (With 4 Examples)

By IncludeHelp Last updated : December 13, 2023

Python numpy.dot() Method

The numpy.dot() method is a built-in method of numpy module, it is used to compute the dot products of two arrays (arr1, arr2). Specifically,

  • If both are the given arrays are 1D arrays, then numpy.dot() method returns the inner product of the vectors (without complex conjugation).
  • If both are the given arrays are 2D arrays, then numpy.dot() method returns matrix multiplication (but using matmul or arr1 @ arr2 is preferred).
  • If either array is a 0-D array (scaler), the numpy.dot() method works similarly to numpy.multiply() method, or arr1 * arr2 is preferred.
  • If arr1 is an N-D array and arr2 is a 1D array, then numpy.dot() method returns the sum product over the last axis of arr1 and arr2.
  • If arr1 is an N-D array and arr2 is an M-D array (where, M>=2), then numpy.dot() method returns the sum product over the last axis of arr1 and the second-to-last axis of arr2.

Module

The following module is required to use the numpy.dot() method:

import numpy as np

Read More: Python NumPy Tutorial

Syntax

The following is the syntax of the numpy.dot() method:

numpy.dot(arr1, arr2, out=None)

Parameter(s)

The following are the parameter(s):

  • arr1: An array-like first argument.
  • arr2: An array-like second argument.
  • out: It's an optional parameter with the default value "None". This is an output argument and must have the exact kind that would be returned.

Return Value

The return type of the numpy.dot() method is <class 'numpy.ndarray'>, it returns the product of both of the given arrays.

Raises

It returns a ValueError, if the last dimension of arr1 is not the same size as the second-to-last dimension of arr2.

Example 1: Find the dot product of scaler values

In this example, we are finding the dot product of two scaler values.

# Importing numpy module
import numpy as np

# Product of scaler values
result = np.dot(2, 4)
print("Dot Product of scalar values : ", result)

# complex values
a = 4 + 8j
b = 2 + 7j

result = np.dot(a, b)
print("Dot Product : ", result)

Output

The output of the above example is:

Dot Product of scalar values :  8
Dot Product :  (-48+44j)

Example 2: Find the dot product of two 1D arrays

In this example, we are finding the dot product of two 1D arrays.

# Importing numpy module
import numpy as np

# Creating two 1D arrays
arr1 = np.array([10, 20, 30])
arr2 = np.array([2, 3, 4])

# Printing the arrays
print("arr1 :\n", arr1)
print("arr2 :\n", arr2)

# Finding dot product of two 1D arrays
result = np.dot(arr1, arr2)
print("\nDot Product (inner product) of two 1D arrays : ", result)

Output

The output of the above example is:

arr1 :
 [10 20 30]
arr2 :
 [2 3 4]

Dot Product (inner product) of two 1D arrays :  200

Example 3: Find the dot product of two 2D arrays

In this example, we are finding the dot product of two 2D arrays.

# Importing numpy module
import numpy as np

# Creating two 2D arrays
arr1 = np.array([[10, 20], [30, 40]])
arr2 = np.array([[1, 2], [3, 4]])

# Printing the arrays
print("arr1 :\n", arr1)
print("arr2 :\n", arr2)

# Finding dot product of two 2D arrays
result = np.dot(arr1, arr2)
print("\nDot Product of two 1D arrays :\n", result)

Output

The output of the above example is:

arr1 :
 [[10 20]
 [30 40]]
arr2 :
 [[1 2]
 [3 4]]

Dot Product of two 1D arrays :
 [[ 70 100]
 [150 220]]

Example 4: Find the dot product of two 2D arrays having different dimensions

In this example, we are finding the dot product of two 2D arrays where their dimensions are different.

# Importing numpy module
import numpy as np

# Creating two arrays of different dimensions
arr1 = np.array([[10, 20], [30, 40], [11, 22]])
arr2 = np.array([[22, 33], [11, 12]])

# Printing the arrays
print("arr1 :\n", arr1)
print("arr2 :\n", arr2)

# Finding dot product of two 2D arrays different dimensions
result = np.dot(arr1, arr2)
print("\nDot Product of two 1D arrays :\n", result)

Output

The output of the above example is:

arr1 :
 [[10 20]
 [30 40]
 [11 22]]
arr2 :
 [[22 33]
 [11 12]]

Dot Product of two 1D arrays :
 [[ 440  570]
 [1100 1470]
 [ 484  627]]

Method Reference: numpy.dot()

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.