How can the Euclidean distance be calculated with NumPy?
Here is a code snippet that shows how to calculate the Euclidean distance using NumPy:
import numpy as np # define two points point1 = np.array([1, 2, 3]) point2 = np.array([4, 5, 6]) # calculate Euclidean distance distance = np.linalg.norm(point1 - point2) print(distance)
This code calculates the Euclidean distance between two points represented as NumPy arrays. The
np.linalg.norm function calculates the Euclidean norm, which is the square root of the sum of the squares of the differences between the coordinates of the two points.
You can also use the numpy.linalg.norm(x, ord=2, axis=None, keepdims=False) function to calculate the distance, it accepts multiple parameters like ord, axis, keepdims etc.