欧几里得距离

定义

在欧几里得空间中,点 x = (x1,...,xn) 和 y = (y1,...,yn)之间的欧氏距离为

向量 x 的自然长度,即该点到原点的距离为

它是一个纯数值。在欧几里得度量下,两点之间线段最短。

Python实现

euclidean_distance.py

from numpy import tile


def point_distance(point_a, point_b):
    d_value = point_b - point_a
    return calculate_distance(d_value)


def point_data_set_distance(point, data_set):
    data_set_size = data_set.shape[0]
    d_value = tile(point, (data_set_size, 1)) - data_set
    return calculate_distance(d_value, True)


def calculate_distance(d, is_set=False):
    axis = 1 if is_set else 0
    return ((d ** 2).sum(axis=axis)) ** 0.5

计算点[1,2,3]到点[5,5,5]之间的距离

>>> from numpy import array
>>> a = array([1,2,3])
>>> b = array([5,5,5])
>>> from euclidean_distance import point_distance
>>> point_distance(a,b)
5.3851648071345037

计算点[1,1]到数据集[[2,2],[3,3],[4,4]]中每个点的距离

>>> from numpy import array
>>> a = array([1,1])
>>> b = array([[2,2],[3,3],[4,4]])
>>> from euclidean_distance import point_data_set_distance
>>> point_data_set_distance(a,b)
array([ 1.41421356,  2.82842712,  4.24264069])

你可能感兴趣的:(欧几里得距离)