Numpy dot product Python : In this tutorial we will see how to use the dot() function of the numpy library.
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.
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)
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)
this result comes from the calculation :
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!