Numpy Dot Product Python

numpy dot product python

Numpy dot product Python : In this tutorial we will see how to use the dot() function of the numpy library.

Overview

The dot product of vectors is the product of two vectors with each other.

The dot product is often used to calculate equations of straight lines, planes, to define the orthogonality of vectors and to make demonstrations and various calculations in geometry. In the physical sciences, it is often widely used. The Numpy library is a powerful library for matrix computation. Thanks to its dot() function, which we will describe in more detail later, it is very easy to compute the dot product of two vectors.

Numpy dot

The dot() function in the Numpy library returns the dot product of two arrays. Its python syntax is as follows:

import numpy as np

result = np.dot(a, a)
 

Depending on the size of the arrays, you would get the following results:

  • If a and b are both 1-D arrays, it is the internal product of the vectors
  • If a and b are both 2D arrays, it is matrix multiplication.
  • If a or b is 0-D (scalar). it is preferable to use the multiply() function
  • If a is an ND array and b is a 1-D array, it is a sum product on the last axis of a and b .
  • If a is an N-D array and b is an M-D array (where M>=2), it is a sum product over the last axis of a and the second-to-last axis of b

Numpy dot Examples

Here are some examples using the dot() function:

import numpy as np

result = np.dot(10, 3)
print(result)
 

Output:

30

It therefore amounts to a simple multiplication

import numpy as np
a = np.array([[4, 1], [2, 5]])
b = np.array([[7, 8], [9, 10]])
result = np.dot(a, b)
print(result)
 

Output:

[[37 42]
[59 66]]

this result comes from the calculation :

[[4*7+1*9,4*8+1*10],[2*7+5*9, 2*8+5*10]]

Conclusion

The dot() function is a very powerful function for matrix calculations and so on. With Numpy, it is very easy to compute the scalar product of two vectors, without going through several calculations that give reflection. Feel free to tell me in comments if you are blocked in the use of this function, I would be pleased to answer as soon as possible.

See you soon for new tutorials!

Back to the Main Menu

Published
Categorized as Python

By ayed_amira

I'm a data scientist. Passionate about new technologies and programming I created this website mainly for people who want to learn more about data science and programming :)

Leave a comment

Your email address will not be published.