# How To Use NumPy dot() Function in Python

In Python NumPy dot() function is used to return the dot product of given arrays. It accepts two arrays as arguments and calculates their dot product. It can handle 2-D arrays but considers them as matrix and will perform matrix multiplication. For N dimensions it is a sum-product similar to matrix multiplication.

In this article, I will explain the Python dot() function and using this syntax how we can find out the dot product of 0-D arrays, 1-D arrays, and 2-D arrays with examples.

## 1. Quick Examples of NumPy dot Product

If you are in a hurry, below are some quick examples of python NumPy dot product.

``````
# Below are the Quick examples
# Example 1: Get the dot product of scalars
arr = 2
arr1 = 5
arr2 = np.dot(arr, arr1)

# Example 2: Get the dot product of complex numbers
arr = 4 + 7j
arr1 = 8 + 9j
arr2 = np.dot(arr, arr1)

# Example 3: Get the dot product of 1-D arrays
arr = np.array([3, 1, 9, 7])
arr1 = np.array([2, 6, 4, 8])
arr2 = np.dot(arr, arr1)

# Example 4: Get the dot product of 2-d arrays
arr = np.array([[3, 1],
[2, 4]])
arr1 = np.array([[5, 2],
[1, 6]])
arr2 = np.dot(arr, arr1)

``````

## 2. Syntax of numpy.dot()

Following is the syntax of dot().

``````
# Use python numpy.dot() syntax
numpy.dot(arr, arr1, out=None)
``````

### 2.1 Parameters of dot()

Following are the parameters of the dot() function.

• `arr` – It defines the first array.
• `arr1` – It defines the second array.
• `out` – This output argument must be a C-contiguous array, and its dtype must be the dtype that would be returned for dot(arr, arr1).

### 2.2 Return Value of the dot()

It returns the dot product of given NumPy arrays. If arr and arr1 are scalars or both the arrays are 1-Dimensional it returns a scalar. Otherwise, it returns an array.

Note: The ValueError occurs when the last dimension of arr is not having the same size as the second-to-last dimension of arr1.

## 3. Usage of NumPy dot() Function

Python NumPy module provides a dot() is a mathematical function and is used to compute the product of two arrays. It returns a scalar or array it depends upon the dimensions of the array.

### 3.1 Get the Dot Product of Two Scalars

If either arr or arr1 is 0-D(scalar) then `numpy.dot(arr, arr1)` is equivalent to `multiplying` two numbers (a * b).

``````
import numpy as np
arr = 2
arr1 = 5

# Get the dot product of scalars
arr2 = np.dot(arr, arr1)
print(arr2)

# Output
# 10
``````

### 3.2 Get the Dot Product of Two Complex Numbers

We can also find the dot product of two complex numbers using `dot()`. For that, we will pass the complex numbers as a parameter to this function and it will return the dot product of two complex numbers.

``````
arr = 4 + 7j
arr1 = 8 + 9j

# Use numpy.dot() function
arr2 = np.dot(arr, arr1)
print(arr2)

# Output
# (-31+92j)
``````

The following is an explanation of how you get this result.

``````
# Take two complex numbers
arr = 4 + 7j
arr1 = 8 + 9j

# Calculation of dot product
4(8 + 9j) + 7j(8 - 9j)
32 + 36j + 56j – 63 # Add real part and add imaginary parts
-31 + 92j
``````

## 4. Get Dot Product of 1-D NumPy Arrays

Let’s take two 1-D arrays and find the dot product of two arrays, it returns a scalar value. First, create an NumPy array using np.array().

``````
# Initialize arrays
arr = np.array([3, 1, 2, 1])
arr1 = np.array([2, 4, 3, 2])

# Get the dot product of 1-D arrays
arr2 = np.dot(arr, arr1)
print(arr2)

# Output:
# Calculation:
# 3*2+1*4+2*3+1*2
# 6+4+6+2
# 18
``````

## 5. Get Dot Product of 2-D Arrays

The dot product of two 2-Dimensional arrays is the same as matrix multiplication, it will return the matrix multiplication of the two input arrays. Let’s take an example,

``````
# Initialize arrays
arr = np.array([[3, 1],
[2, 4]])
arr1 = np.array([[5, 2],
[1, 6]])

# Get the dot product of 2-d arrays
arr2 = np.dot(arr, arr1)
print(arr2)

# Output
# [[16 12]
# [14 28]]
``````

The following calculation is shown 2-D matrix multiplication.

``````
# Output
[[3*5+1*1, 3*2+1*6],[2*5+4*1, 2*2+4*6]]

[[16 12]
[14 28]]

``````

## 6. Conclusion

In this article, I have explained the Python NumPy `dot()` function and using this how we can get the dot product of scalar values, 1-D arrays, and 2-D arrays with examples.

Happy Learning!!

## References 