A vector in Python is a data structure representing a sequence of elements. It is used to store numeric data but can also store other types of data, such as strings.
What is Vector distance in Python?
Vector distance, also known as vector norm, is a measure of the length of a vector in a vector space. The distance of a vector is a measure of the magnitude of the vector, a mathematical concept that describes the size and direction of a vector.
Python distance between two vectors
4 ways to calculate the distance between two vectors in Python.
- Using the euclidean() function.
- Using the np.linalg.norm() function.
- Using the np.dot() function.
- Using square() and sum().
Method 1: Use the euclidean() function
To calculate the distance between two vectors in Python, use the euclidean() function from the scipy.spatial.distance module. The euclidean() function calculates the Euclidean distance, which is the straight-line distance between two points in n-dimensional space.
To use the euclidean() function in a Python file, import the scipy library and if it is not installed on your machine, then type the following command to install it.
python3 -m pip install scipy
Now, you can use all the functions from the scipy library.
Here is an example of calculating the distance between two vectors.
from scipy.spatial.distance import euclidean
# Define two vectors as lists of numbers
vector_1 = [11, 21, 31]
vector_2 = [41, 51, 61]
# Calculate the distance between the two vectors using the euclidean() function
distance = euclidean(vector_1, vector_2)
print(distance)
Output
51.96152422706632
The distance between two vectors in the above example is: 51.96152422706632
.
In this example, we created two vectors by initializing two lists and then used the euclidean() function from the scipy library to calculate the distance.
Method 2: Use np.linalg.norm() function
To calculate the distance between two vectors using numpy, use the np.linalg.norm() function. The numpy module provides a linalg.norm() function that calculates the Euclidean norm of a vector, which is the square root of the sum of the squares of the vector’s components.
Use the np.linalg.norm() function to calculate the distance between two vectors by taking the difference between the vectors and calculating the distance between the vectors.
import numpy as np
# Define two vectors as array of numbers
vector_1 = np.array([11, 21, 31])
vector_2 = np.array([41, 51, 61])
# Calculate the difference between the vectors
difference = vector_1 - vector_2
# Calculate the distance of two vectors
distance = np.linalg.norm(difference)
print(distance)
Output
51.96152422706632
Method 3: Using the dot() method
To calculate the distance of two vectors using the np.dot() method, calculate the difference between two vectors using subtraction, and use the np.dot() function to calculate the dot product of two vectors in which one is the transpose of the subtraction output. One is subtraction output, and the np.sqrt() function calculates the distance between two vectors.
import numpy as np
# Define two vectors as arrays of numbers
vector_1 = np.array([11, 21, 31])
vector_2 = np.array([41, 51, 61])
# Calculate the difference between the vectors
difference = vector_1 - vector_2
# Calculate the distance of two vectors
dot_product = np.dot(difference.T, difference)
# printing Euclidean distance between two vectors using np.sqrt()
print(np.sqrt(dot_product))
Output
51.96152422706632
Method 4: Using square() and sum() functions
To calculate the sum of squares in Python, apply the np.sum() method on the result of np.square(), which takes the subtraction of vector 1 and vector 2.
Apply the np.sqrt() function on the result of the np.sum() method, and you will get the distance between two vectors.
import numpy as np
# Define two vectors as arrays of numbers
vector_1 = np.array([11, 21, 31])
vector_2 = np.array([41, 51, 61])
# Calculating the sum of squares
distance = np.sum(np.square(vector_1 - vector_2))
# printing Euclidean distance using np.sqrt() function
print(np.sqrt(distance))
Output
51.96152422706632
And we get the same output as the above three outputs on three different approaches.
Conclusion
The best and an efficient way to calculate the distance between two vectors in Python is to use the euclidean() function provided by the scipy library.
If you are working with an array, use the np.linalg.norm() function.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has experience with various programming languages and technologies, including PHP, Python, and JavaScript. He is comfortable working in front-end and back-end development.