Summary: In this tutorial, you will learn to calculate the angle between two vectors using Python Programming language.
Getting the angle between two vectors returns the angle in radians between two vectors of equal dimension.
In Python, you can easily find angle using the
Use numpy.arccos() to get the Angle Between Two Vectors
import numpy as np #vectors v_1 = [0, 1] v_2 = [1, 0] #unit of vectors unit_v_1 = v_1 / np.linalg.norm(v_1) unit_v_2 = v_2 / np.linalg.norm(v_2) #dot product of vectors dot_product = np.dot(unit_v_1, unit_v_2) #angle between two vectors angle = np.arccos(dot_product) print(angle) #90 degrees in radian
This method requires you to use lists to represent vectors.
In the program, we have used
np.linalg.norm(vector) to get the unit vector of
vector. We did this for both vectors.
Then, we called
np.dot(v1, v2) method to get the dot product of the previous results
Finally, we called
np.arccos(x) method with the previous result as
x to get the angle between the two original vectors.
So, this is how you can get the angle between two given vectors in Python.