Projection of Vector a on b in Numpy : Find Vector Projection in Python

Find Vector Projection in Python

Vector projection is used for determining the component of a vector along with a specific direction. There are many real-life examples of vector projection. For example, you can use it for finding the emotions of a human being or the character of the person. In this entire tutorial, you will know how to calculate Projection of Vector a on b in Numpy using the various methods.

Method to Find Projection of Vector a on b in Numpy

Numpy is a great python package for creating an array and computing complex mathematical calculations. There are many inbuilt functions for that. You will use all the methods for finding Projection of Vector a on b using Numpy.

Method 1: Find Projection using formulae

The first method you will know is the use of Vector Projection formulae. If you know the formulae then it’s good. But if you don’t know then below is the formulae.

Formulae for the Projection of vector a on b
Formulae for the Projection of vector a on b
Projection of vector a on b
Projection of vector a on b
source: https://en.wikipedia.org/wiki/Vector_projection#/media/File:Projection_and_rejection.png

Now let’s find the Projection of Vector a on b in Numpy. Use the below lines of code to calculate vector projection in python.

import numpy as np
a = np.array([10,-20,30])
b = np.array([30,15,-40])
vp = (np.dot(a, b) / np.dot(b, b)) * b
print(vp)

Output

Finding the Projection of vector a on b using the formulae
Finding the Projection of vector a on b using the formulae

Explanation of  the code

Here I have to use the projection formulae as the above. But to find the dot product of a and b I have used np.dot() method provided by the NumPy package. I have divided the dot product of a and b by the dot product of b with b itself. After that multiply the result with the b vector for specifying the direction.

Method 2: Finding the Projection using  the normalization

The other method to find the projection of a on b in NumPy is using the normalization of the second vector. You can find the normalization of the vector you will use the inbuilt np.linalg.norm() method.

Execute the below lines of code.

import numpy as np
a = np.array([10,-20,30])
b = np.array([30,15,-40])
vp = (np.dot(a, b) / np.linalg.norm(b)**2 ) * b
print(vp)

Output

Finding the Projection by normalizing the other vector
Finding the Projection by normalizing the other vector

The answer will be the same if you will compare the results for both methods.

Conclusion

There are many useful vector projections in real-life. These are the methods for finding the projection of a vector with another vector. You can use any one of them.

I hope you have liked this tutorial. If you have any query regarding this then you can contact us for more help.

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner