| by Arround The Web | No comments

Numpy Dot Product

In Python, the “NumPy” library is employed for numerical computation. This library provides several modules and functions to perform numerical operations in Python. The “numpy.dot()” function of Python “NumPy” library is utilized to determine the dot product of the given arrays. This Python tutorial will explain a complete guide on the “numpy.dot()” function using appropriate examples.

This article covers the below topics:

What is the Python “numpy.dot()” Product Function?

The “numpy.dot()” function computes the dot product of the particular two arrays. If both arrays are “1-dimensional”, the inner product of vectors is calculated (without complex conjugation). However, in the case of a “2-dimensional” array, it carries out matrix multiplication.

Syntax

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

In the above syntax:

  • The “arr” parameter specifies the first array.
  • The “arr1” parameter indicates the second array.
  • The “out” is an optional parameter that is utilized for performance. The array must be C-contiguous, and the “dtype” must be the “dtype” returned by “dot(a,b)”.

Return Value

This function retrieves the dot product of the two given vectors. The function returns an array if both vectors are not scalars or “1-D” arrays. If both vectors are scalars or “1-D” arrays, then a scalar is returned instead.

Example 1: Calculating the NumPy Dot Product of Two Scalars

The below code is utilized to compute the dot product of given two “0-D” scalars:

import numpy

print(numpy.dot(15,5))

In the above code, the “numpy.dot()” function is utilized to determine the dot product of the two given scalar values.

Output


As observed, the NumPy dot product has been calculated.

Example 2: Calculating the NumPy Dot Product of Input Two 1-D Arrays

Overview of the following code to calculate the product of “1-D” arrays:

import numpy

X1 = numpy.array([10, 20, 30, 40])

Y1 = numpy.array([15, 25, 35, 45])

print(numpy.dot(X1, Y1))

In the above code:

  • The “numpy” module is imported and the “numpy.array()” function creates two “1-D” arrays.
  • The “numpy.dot()” function takes the two “1-D” arrays as its arguments, calculates the dot product, and returns the corresponding scalar value.

Output

Example 3: Calculating the NumPy Dot Product of Input Two 2-D Arrays

Go through the below code lines to evaluate the 2-D arrays for the product:

import numpy

X1 = numpy.array([[10, 20], [30, 40]])

Y1 = numpy.array([[15, 25], [35, 45]])

print(numpy.dot(X1, Y1))

According to the above code, the “numpy.dot()” function is utilized to determine/calculate the dot product of the given “2-D” arrays. This function retrieves the matrix multiplication of the given two “2-D” arrays.

Output

The NumPy dot product has been calculated accordingly in this case as well.

Example 4: Calculating the NumPy Dot Product of Two Complex Numbers

This example determines the dot product of the two specified complex numbers:

import numpy

X1 = 5 + 10j

Y1 = 8 + 20j

print(numpy.dot(X1, Y1))

According to the above code, the “numpy.dot()” function takes the defined complex numbers as its arguments and returns their dot product.

Output

The dot product of the initialized complex numbers has been shown in the above output.

Conclusion

The “numpy.dot()” function is used in Python to determine the dot product of “Complex Numbers”, the “1-D” or “2-D” arrays, and “Scalar Values”. This function returns the scalar dot product value if the initialized array is scalar, “0-D” or “1-D”. However, it retrieves the matrix multiplication of the defined “2-D” array value. This Python guide presented a complete guide on the “numpy.dot()” function using multiple examples.

Share Button

Source: linuxhint.com

Leave a Reply