📅  最后修改于: 2023-12-03 14:55:53.512000             🧑  作者: Mango
欧式距离,又称欧几里得距离(Euclidean Distance),指在m维空间中两个点之间的真实距离,即两点之间的直线距离。
在Python中,我们可以很方便地使用NumPy库来计算欧式距离。
import numpy as np
def euclidean_distance(x, y):
"""
Calculate the Euclidean distance between two points in n-dimensional space.
x: array_like
An m-dimensional array_like object representing a point.
y: array_like
An m-dimensional array_like object representing a point.
Returns
-------
float
The Euclidean distance between `x` and `y`.
Examples
--------
>>> euclidean_distance([0,0], [3,4])
5.0
>>> euclidean_distance([1,1,1], [2,2,2])
1.7320508075688772
"""
x = np.array(x)
y = np.array(y)
return np.sqrt(np.sum((x - y) ** 2))
# Example usage
x = [0, 0]
y = [3, 4]
print(euclidean_distance(x, y)) # 5.0
欧式距离是一种常用的距离度量方法,Python中可以借助NumPy库实现计算。