In Python NumPy `dot()`

function is used to compute dot products of two given arrays. It accepts two arrays as arguments and calculates their dot product. It can handle 2-D arrays but considers them as matrices 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 NumPy `dot()`

method syntax, parameters, and usage of how to find out the dot product of 0-D arrays, 1-D arrays, and 2-D arrays with examples.

## 1. Quick Examples of dot() Function

If you are in a hurry, below are some quick examples of the dot() function.

```
# Quick examples of dot() function
# Example1: Calculate the dot product of scalars
arr = 2
arr1 = 5
arr2 = arr * arr1
# Example 2: Get the dot product of scalars
arr2 = np.dot(arr, arr1)
# Example 3: Use numpy.dot() function
# Get the dot product of two complex numbers
arr = 4 + 7j
arr1 = 8 + 9j
arr2 = np.dot(arr, arr1)
# Example 4: Using numpy.dot()
# Calculate 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 5: Calculate dot product 2D arrays
# Using numpy.dot()
arr = np.array([[3, 1],[2, 4]])
arr1 = np.array([[5, 2],[1, 6]])
arr2 = np.dot(arr, arr1)
```

## 2. Syntax of 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`

– The first input array. It can be a scalar, vector, or matrix.`arr1`

– The second input array. It can be a scalar, vector, or matrix.`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

To calculate the dot product of two scalar values using NumPy. However, the dot product is defined for vectors and matrices, not individual scalar values. The dot product of two vectors is calculated as the sum of the products of their corresponding elements.

If you want to calculate the dot product of two scalar values, you can simply multiply them together without using NumPy. In this case, `arr2`

will be equal to 10, which is the result of multiplying 2 and 5 together.

```
# Import numpy
import numpy as np
# Two scalar values
arr = 2
arr1 = 5
# Calculate the dot product of scalars
arr2 = arr * arr1
print("After getting the dot product:\n", arr2)
# Get the dot product of scalars
arr2 = np.dot(arr, arr1)
print("After getting the dot product:\n",arr2)
```

Yields below output.

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

You can also find the dot product of two complex numbers using `dot()`

. For that, you will pass the complex numbers as a parameter to this function and it will return the dot product of two complex numbers.

```
import numpy as np
# Two complex numbers
arr = 4 + 7j
arr1 = 8 + 9j
# Use numpy.dot() function
# Get the dot product of two complex numbers
arr2 = np.dot(arr, arr1)
print("After getting the dot product two complex numbers:\n",arr2)
# Output:
# After getting the dot product two complex numbers:
# (-31+92j)
```

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

```
# Take two complex numbers
arr = 4 + 7j
arr1 = 8 + 9j
# Output:
# 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

To get the dot product of 1-D NumPy arrays, you can use the `numpy.dot()`

function. 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().

```
import numpy as np
# Define 1-D NumPy arrays
arr = np.array([3, 1, 2, 1])
arr1 = np.array([2, 4, 3, 2])
# Using numpy.dot()
# Calculate dot product of 1-D arrays
arr2 = np.dot(arr, arr1)
print("After getting the dot product of a 1-D arrays:\n",arr2)
# Output:
# After getting the dot product of a 1-D arrays:
# 18
# Calculation:
# 3*2+1*4+2*3+1*2
# 6+4+6+2
# 18
```

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

To get the dot product of 2-D NumPy arrays, you can use the `numpy.dot()`

function. 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,

```
import numpy as np
# Define 2-D NumPy arrays
arr = np.array([[3, 1],[2, 4]])
arr1 = np.array([[5, 2],[1, 6]])
# # Calculate dot product using numpy.dot()
arr2 = np.dot(arr, arr1)
print("After getting the dot product of a 2-D arrays:\n",arr2)
```

Yields below output. 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]]
# After getting the dot product of a 2-D arrays:
[[16 12]
[14 28]]
```

## Frequently Asked Questions

**What is the numpy.dot() function used for?**The `numpy.dot()`

function is used in NumPy to compute the dot product of two arrays. It performs matrix multiplication for 2-D arrays and behaves as a sum product for arrays with more than two dimensions.

**Can numpy.dot() be used for complex numbers?**The `numpy.dot()`

can be used for complex numbers as well. It performs the dot product operation for complex arrays in the same way it does for real arrays.

**How is the dot product calculated for 1-D arrays?**The dot product of two 1-D arrays (vectors) is calculated by taking the sum of the element-wise products of the corresponding elements in the arrays.

**Can numpy.dot() be used for multiplying a matrix and a vector?**The `numpy.dot()`

can be used for multiplying a matrix and a vector. When one of the inputs is a matrix and the other is a 1-D array (vector), `numpy.dot()`

performs the dot product operation, resulting in a 1-D array. The elements of the resulting vector are calculated by taking the dot product of each row of the matrix with the elements of the vector.

**How does numpy.dot() handle higher-dimensional arrays?**When dealing with higher-dimensional arrays (tensors) using `numpy.dot()`

, the behavior depends on the dimensions of the input arrays. For arrays with more than two dimensions, `numpy.dot()`

behaves as a sum product over the last axis of the first array and the second-to-last axis of the second array. This operation is applied recursively along the specified axes.

**Can I use numpy.dot() with non-numeric arrays?**The `numpy.dot()`

function is specifically designed for numerical arrays (arrays containing numeric data) and is not intended to be used with non-numeric arrays. If you attempt to use `numpy.dot()`

with non-numeric arrays, it will raise a TypeError.

## Conclusion

In this article, I have explained the Python NumPy `dot()`

function and using this how you can get the dot product of scalar values, 1-D arrays, and 2-D arrays with examples.

Happy Learning!!

## Related Articles

- How to get square values of an array?
- How to create NumPy one’s array?
- How to use NumPy delete() Function
- How to use NumPy divide() Function
- How to get maximum of NumPy array?
- Python NumPy Interpolate Function
- How to Check NumPy Array Equal?
- How to Transpose Matrix in NumPy
- NumPy Element Wise Multiplication
- How to Convert NumPy Matrix to Array