Python Numpy Dot Product | Explained

Python Supports multiple modules and functions for performing various kinds of mathematical computation. “Numpy” is the most popular package in Python for Numerical computation. Using different “Numpy” functions in programming can increase the productivity and efficiency of the Program.

This write-up will cover an in-depth overview of the Python Numpy function named “np.dot()” to calculate the dot product of two arrays.

The following outcomes are explained in this Python article:

So let’s begin!

What is a Numpy Dot Product in Python?

How to install Kali Linux in Window...

Scalar products are also referred to as “dot products” in Mathematics. This algebraic operation takes two equal values of a scalar or vector number and returns a single particular number.

The “Numpy” module offers an “np.dot()” function, which is used to calculate the dot product of any two scaler numbers or arrays. The syntax of the “np.dot()” function is shown below:

numpy.dot(x, y, out=None)

In the above syntax:

  • The “x” parameter takes the first value of an array.
  • The “y” parameter takes the second value of an array.
  • The “out” parameter is optional and used for performance.

The following points provide the outcomes of specific input given into the function of “np.dot()”:

  • If both input variables are scalar quantities or zero-dimensional, then the output of the “np.dot()” function is the multiplication of two numbers.
  • If the input arrays are 1-dimensional, then the “np.dot()” function returns the inner product of the vector.
  • If the input arrays are 2-dimensional, then the “np.dot()” function returns the matrix multiplication of the given arrays.
  • If either parameter “x” or“y” is n-dimensional, then the “np.dot()” function returns the sum of the product of x and y.
  • If the parameter values of the given function are “n and m” dimensional. The “numpy.dot()” function retrieves the product sum over the ‘last axis’ of the first number and the second to last axis of the second number.

Example 1: Numpy Dot Product of Scalar and Vector Quantity

In the following example, the “np.dot()” function is used to create a dot product of two scalars and vector quantities. The 2d vector quantity is initialized in the form of a complex number.

Code:

import numpy as np

# numpy dot product of Scalars
dot_product = np.dot(10, 10)
print("Dot Product of scalar: ", dot_product)

# numpy dot product of vectors
vector_1 = 2 + 3j
vector_2 = 4 + 5j

dot_product = np.dot(vector_1, vector_2)
print("Dot Product of vectors: ", dot_product)

In the above code:

  • The “NumPy” library is imported as “np” at the start of Program.
  • Firstly, the “np.dot()” function finds the dot product of two scalar numbers by taking its value as an argument.
  • Secondly, two values of the 1d array are initialized as a vector quantity and stored in a variable.
  • The “np.dot()” function takes the variable value as an argument and returns the dot product of the 1d-arrays vector.

Output:

The above output shows the dot product of the scalar and 1d-array vector.

Example 2: Numpy Dot Product of 1D Array

In the following example, the function “np.dot()” is used to calculate the dot product of a 1-d array. Lets understand the concept by the following code:

Code:

import numpy as np

Number_1 = np.array([4, 7, 15])
Number_2 = np.array([7, 3, 17])

#dot product
Result = np.dot(Number_1, Number_2)
print('Dot Product: ',Result)

In the above code:

  • Two 1d-arrays are generated using the function named “np.array()”.
  • The “np.dot()” takes the variable value as a parameter value and returns the dot product of the following 1d-array.

Output:

The output shows the dot product of the “1-D” array.

The mathematical calculation of the dot product of a “1-D” array is shown below:

Example 3: Numpy Dot Product of 2D Array

In the following example, the NumPy module function named “np.dot()” takes the 2-d variable value as input and returns the dot product.

Code:

import numpy as np

Number_1 = np.array([[12, 11], [15, 14]])
Number_2 = np.array([[13, 14], [17, 18]])

#dot product
dot_product = np.dot(Number_1, Number_2)
print(dot_product)

In the above code:

  • The function named “np.array()” is used here to create 2-D arrays.
  • The value of 2-D arrays stored in a variable named “Number_1” and “Number_2”.
  • The “np.dot()” function takes the variable value inside the parentheses and returns the dot product of the 2-D arrays.

Output:

The above output shows the dot product of the “2-D” array.

The mathematical calculation of the dot product of a “2-D” array is shown below:

That’s all from this Python!

Conclusion

In Python, the “np.dot()” function of the “Numpy” package is used to calculate the dot product of scalar, vectors, 1-D arrays, and 2-D arrays. The “np.dot()” function returns the scalar value if both input numbers are scalar or 1-Dimensional. When a “1-D” or “2-D” array is passed into the “np.dot()” function then it returns the output as inner product multiplication and matrix multiplication. This write-up provided all the details related to the Numpy dot product with multiple examples.