Hello programmers, in this article, we will discuss the Numpy dot products in Python. Numpy dot() function computes the dot product of Numpy n-dimensional arrays. The numpy.dot () function accepts two numpy arrays as arguments, computes their dot product, and returns the result. For 1D arrays, it is the inner product of the vectors. It performs dot product over 2 D arrays by considering them as matrices. Hence performing matrix multiplication over them. We will look into the implementation of numpy.dot() function over scalar, vectors, arrays, and matrices. Before that, let me just brief you with the syntax and return type of the Numpy dot product in Python.

Contents of Tutorial

## Syntax of numpy dot() function

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

**Parameters:**

- vector_a = It is the first argument(array) of the dot product operation. If
- vector_b = Second argument(array). If it is complex, its complex conjugate is used. [mandatory]
- out = It is a C-contiguous array, with datatype similar to that returned for dot(vector_a,vector_b). [optional]

## Return type of Numpy Dot() function

- If ‘a’ and ‘b’ are scalars, the dot(,) function returns the multiplication of scalar numbers, which is also a scalar quantity.
- For ‘a’ and ‘b’ as 1-dimensional arrays, the dot() function returns the vectors’ inner product, i.e., a scalar output.
- For ‘a’ and ‘b’ as 2 D arrays, the dot() function returns the matrix multiplication. The output returned is array-like.
- If ‘a’ is nd array, and ‘b’ is a 1D array, then the dot() function returns the sum-product over the last axis of a and b. (Output is an
**array**) - If ‘a’ is an M-dimensional array and ‘b’ is an N-dimensional array, then the dot() function returns an
**array**that is the sum-product over the last axis of ‘a’ and the second-to-last axis of ‘b

## Scalar Numpy dot product

import numpy as np a = 3 b = 6 output = np.dot(a,b) print(output)

**Output:**

18

**Explanation:**

In the above example, two scalar numbers are passed as an argument to the np.dot() function. This numpy dot function thus calculates the dot product of two scalars by computing their multiplication. Passing a = 3 and b = 6 to np.dot() returns 18.

## Numpy dot product of complex vectors

import numpy as np vector_a = 2 + 3j vector_b = 5 + 4j product = np.dot(vector_a, vector_b) print("Dot Product : ", product)

**Output:**

Dot Product : (-2 + 23j)

**Explanation:**

In the above example, the numpy dot function is used to find the dot product of two complex vectors. Since vector_a and vector_b are complex, complex conjugate of either of the two complex vectors is used.

Here the complex conjugate of vector_b is used i.e., (5 + 4j) and (5 _ 4j).

The np.dot() function calculates the dot product as : 2(5 + 4j) + 3j(5 – 4j)

#complex conjugate of vector_b is taken = 10 + 8j + 15j – 12 = -2 + 23j

Thus, passing vector_a and vector_b as arguments to the np.dot() function, (-2 + 23j) is given as the output.

## Numpy dot product of 1D Arrays

import numpy as np #initialize arrays A = np.array([3, 1, 7, 4]) B = np.array([2, 4, 5, 8]) #dot product output = np.dot(A, B) print(output)

**Output:**

77

**Explanation**:

Firstly, two arrays are initialized by passing the values to np.array() method for A and B. The A and B created are one dimensional arrays. The numpy dot function calculates the dot product for these two 1D arrays as follows:

[3, 1, 7, 4] . [2, 4, 5, 8] = 3*2 + 1*4 + 7*5 + 4*8 = 77

Thus by passing A and B one dimensional arrays to the np.dot() function,

a scalar value of 77 is returned as the ouput.

## Dot product over 2D arrays

import numpy as np #initialize arrays A = np.array([[2, 1], [5, 4]]) B = np.array([[3, 4], [7, 8]]) #dot product output = np.dot(A, B) print(output)

**Output:**

[[13 16], [43 52]]

**Explanation:**

Two arrays – A and B, are initialized by passing the values to np.array() method. The A and B created are two-dimensional arrays. The dot product of two 2-D arrays is returned as the matrix multiplication of those two input arrays. The dot product for 3D arrays is calculated as:

`= [[2, 1], [5, 4]].[[3, 4], [7, 8]]`

`= [[2*3+1*7, 2*4+1*8], [5*3+4*7, 5*4+4*8]]`

`= [[13, 16], [43, 52]`

Thus passing A and B 2D arrays to the np.dot() function, the resultant output is also a 2D array.

## Conclusion

In this article we learned how to find dot product of two scalars and complex vectors. We also learnt the working of Numpy dot function on 1D and 2D arrays with detailed examples. Refer to this article for any queries related to the Numpy dot product in Python.

However, if you have any doubts or questions do let me know in the comment section below. I will try to help you as soon as possible.

*Happy Pythoning!*