Free cookie consent management tool by TermsFeed The dot() function of NumPy module | Pythontic.com

The dot() function of NumPy module

Overview:

  • The dot() function returns the dot product of two NumPy arrays.

Example 1 - Dot product of two 1-dimensional arrays:

  • As both the input arrays in the example are one-dimensional the return value is a scalar.

# Example Python program that finds the dot product of
# two one dimensional numpy arrays using numpy.dot() 
# function
import numpy

# Create array1
n1 = numpy.array([1, 2, 3], numpy.int32)

# Create array2
n2 = numpy.array([4, 5, 6], numpy.int32)

# Find the dot product of two arrays
dotProduct = numpy.dot(n1, n2)
print(dotProduct)

Output:

32

Example 2 - Dot product of two 2-dimensional arrays:

# Example Python program that multiplies two 2-dimensional 
# matrices using the NumPy function dot().
import numpy

# Create matrix1
n1        = numpy.arange(9)
array1     = n1.reshape(3, 3)

# Create matrix2
n2         = numpy.arange(9)
array2     = n2.reshape(3, 3)

dotProduct = numpy.dot(array1, array2)
print(array1)
print(array2)
print(dotProduct)

Output:

[[0 1 2]

 [3 4 5]

 [6 7 8]]

[[0 1 2]

 [3 4 5]

 [6 7 8]]

[[ 15  18  21]

 [ 42  54  66]

 [ 69  90 111]]

 


Copyright 2025 © pythontic.com