numpy
  1. numpy-numpydot

NumPy dot()

NumPy dot() is an inbuilt function that returns the dot product of two arrays. It is used to calculate the dot product of two arrays. If the dimensions of the two input arrays are different, then np.dot() invokes broadcasting mechanism to make it compatible for the dot product and return the resultant.

Syntax

numpy.dot(array1, array2, out = None)

Example

import numpy as np

arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])

dot_product = np.dot(arr1, arr2)

print("Dot Product of two 1-D arrays is : ", dot_product)

Output

Dot Product of two 1-D arrays is : 32

Explanation

In the above example, we have two 1-D arrays arr1 and arr2. We find the dot product of these arrays using the numpy.dot() method. The dot product of two 1-D arrays with the same number of elements, is equivalent to the inner product of two vectors. Hence, the output of the above example is the dot product of the two arrays.

Use

The numpy.dot() method is used to compute the dot product of two arrays in NumPy. It can be used to multiply two or more arrays and get the resulting array. This is particularly useful in matrix multiplication and is widely used in Machine Learning.

Important Points

  • The numpy.dot() method calculates the dot product of two arrays.
  • If the two input arrays are multidimensional, then the dot product is calculated for the last dimension of the first array and the second-last dimension of the second array.
  • If either of the two input arrays is a scalar, it is equivalent to an array of the same shape as the other input array with every element equal to the scalar value.
  • The output of the function will be a scalar if the two input arrays are 1-D arrays with the same number of elements.

Summary

numpy.dot() is a powerful function in NumPy that can be used to calculate the dot product of two arrays. It is particularly useful in matrix multiplication and is widely used in Machine Learning. The method is easy to use and accepts two arrays as input parameters. However, one should be careful while working with arrays of different dimensions and keeping track of the output dimensions.

Published on: