What is the numpy.dot() Method

Numpy.dot() method returns the dot product of vectors a and b.” It can handle 2D arrays but considers them as matrices and will perform matrix multiplication.

Syntax

numpy.dot(vector_a, vector_b, out = None)

Parameters

The dot() function takes mainly three parameters:

  1. vector_a: This is the first vector.
  2. vector_b: This is the second vector.
  3. out: Argument Production. This must have the same sort that would be returned unless used. Specifically, it must have the appropriate form, must be C-contiguous, and its dtype must be the form returned for dot(a, b). That is a feature of the performance. Therefore, if those conditions are not met, instead of attempting to be flexible, an exception is made.

Return Value

The numpy.dot() method returns the dot product of two given vectors. If any of the vectors or both vectors are complex, then its complex conjugate calculates the dot product.

Example 1: How to Use numpy.dot() method

# Program to show working of numpy.dot
# When both the vectors are 1D

# Importing numpy
import numpy as np

# We will create an 1D array
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([2, 3, 4, 5])
# Printing the array
print("The first array is: ", arr1)
print("The second array is: ", arr2)
# Shape of the array
print("Shape of the first array is : ", np.shape(arr1))
print("Shape of the second array is : ", np.shape(arr2))

# Printing dot product of the arr1.arr2
out = np.dot(arr1, arr2)
print("Dot product of arr1 and arr2")
print(out)

# When both are complex
a = 6+1j
b = 4+3j
out1 = np.dot(a, b)
print("Dot product of a and b")
print(out1)

Output

The first array is: [1 2 3 4]
The second array is: [2 3 4 5]
Shape of the first array is : (4,)
Shape of the second array is : (4,)
Dot product of arr1 and arr2
40
Dot product of a and b
(21+22j)

We can see that we got the answer 40. According to the rule of the dot product, it did like this way.

(1*2+2*3+3*4+4*5) = 40.

Example 2

# Program to show the working of numpy.dot
# When both the vectors are 2D

# Importing numpy
import numpy as np

# We will create an 1D array
arr1 = np.array([[1, 2], [5, 6]])
arr2 = np.array([[2, 3], [2, 4]])
# Printing the array
print("The first array is:\n ", arr1)
print("\nThe second array is: \n ", arr2)
# Shape of the array
print("Shape of the first array is : ", np.shape(arr1))
print("Shape of the second array is : ", np.shape(arr2))

# Printing dot product of the arr1.arr2
out = np.dot(arr1, arr2)
print("Dot product of arr1 and arr2")
print(out)

Output

The first array is:
 [[1 2]
 [5 6]]

The second array is:
 [[2 3]
 [2 4]]
Shape of the first array is : (2, 2)
Shape of the second array is : (2, 2)
Dot product of arr1 and arr2
[[ 6 11]
 [22 39]]

Example 3: Get the Dot Product of Two Scalars

import numpy as np
arr = 11
arr1 = 19

arr2 = np.dot(arr, arr1)
print(arr2)

Output

209

Example 4: Get the Dot Product of Two Complex Numbers

import numpy as np

arr = 21 + 19j
arr1 = 19 + 21j

arr2 = np.dot(arr, arr1)
print(arr2)

Output

(-31+92j)

Example 5: Get Dot Product of 2D Arrays

import numpy as np

arr1 = np.array([[11, 21], [19, 46]])
arr2 = np.array([[1, 2], [3, 4]])

arr = np.dot(arr1, arr2)
print(arr)

Output

[[ 74 106]
 [157 222]]

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.