AppDividend
Latest Code Tutorials

# Numpy dot: How to Use np dot() Function in Python

Numpy dot() method returns the dot product of two arrays. For 2D vectors, it is equal to matrix multiplication. For 1D arrays, it is the inner product of the vectors. For N-dimensional arrays, it is the sum-product over the last axis of a and the second-last axis of b.

## Numpy dot()

Numpy dot() is a mathematical function that is used to return the mathematical dot of two given vectors (lists). The vectors can be single dimensional as well as multidimensional. In both cases, it follows the rule of the mathematical dot product.

### Syntax

`numpy.dot(vector_a, vector_b, out = None)`

### Parameters

The dot() function takes mainly three parameters:

1. vector_a: This is the first vector.
2. vector_b: This is the second vector.
3. out: Argument Production. This must have the same sort that would be returned unless it was used. Specifically, it must have the appropriate form, must be C-contiguous, and its dtype must be the form returned for dot(a, b). That is a feature of the performance. Therefore, if those conditions are not met, instead of attempting to be flexible, an exception is made.

### Return Value

The numpy.dot() method returns the dot product of two given vectors. If any of the vectors or both the vectors are complex, then it’s complex conjugate is used to calculate the dot product.

### Calculate the dot when given vectors are single-dimensional

See the following code.

```# Program to show working of numpy.dot
# When both the vectors are 1D

# Importing numpy
import numpy as np

# We will create an 1D array
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([2, 3, 4, 5])
# Printing the array
print("The first array is: ", arr1)
print("The second array is:  ", arr2)
# Shape of the array
print("Shape of the first array is : ", np.shape(arr1))
print("Shape of the second array is : ", np.shape(arr2))

# Printing dot product of the arr1.arr2
out = np.dot(arr1, arr2)
print("Dot product of arr1 and arr2")
print(out)

# When both are complex
a = 6+1j
b = 4+3j
out1 = np.dot(a, b)
print("Dot product of a and b")
print(out1)```

#### Output

```The first array is:  [1 2 3 4]
The second array is:   [2 3 4 5]
Shape of the first array is :  (4,)
Shape of the second array is :  (4,)
Dot product of arr1 and arr2
40
Dot product of a and b
(21+22j)```

#### Explanation

In this program, in the first case, we have initialized two 1D arrays, and then we have printed both the arrays and their shape also. After that, we have called numpy.dot() to calculate the dot product of the arrays.

We can see that we got the answer 40. According to the rule of the dot product, it did like this way

(1*2+2*3+3*4+4*5) = 40.

Also, in the second case, we have declared two complex equations. Then we printed dot products of them. We can see that the answer is (21+22j), which was calculated in this way:

6*(4+3j) + 1j*(4-3j)

= 24+18j+4j-3

= 21+22j

### Calculate the dot product given vectors are multidimensional

See the following code.

```# Program to show the working of numpy.dot
# When both the vectors are 2D

# Importing numpy
import numpy as np

# We will create an 1D array
arr1 = np.array([[1, 2], [5, 6]])
arr2 = np.array([[2, 3], [2, 4]])
# Printing the array
print("The first array is:\n ", arr1)
print("\nThe second array is: \n ", arr2)
# Shape of the array
print("Shape of the first array is : ", np.shape(arr1))
print("Shape of the second array is : ", np.shape(arr2))

# Printing dot product of the arr1.arr2
out = np.dot(arr1, arr2)
print("Dot product of arr1 and arr2")
print(out)```

#### Output

```The first array is:
[[1 2]
[5 6]]

The second array is:
[[2 3]
[2 4]]
Shape of the first array is :  (2, 2)
Shape of the second array is :  (2, 2)
Dot product of arr1 and arr2
[[ 6 11]
[22 39]]```

#### Explanation

In this program, in the first case, we have initialized two 2D arrays, and then we have printed both the arrays and their shape also. After that, we have called numpy.dot() to calculate the dot product of the arrays.

We can see that the answer is in the 2D array. According to the rule of the dot product, we got the answer.

That is it for the Numpy dot() method.