查找质心和点之间的距离

Find distance between a centroid and a point

假设我有这个点特征列表 [3.5, 2.5, 7.5] 和 class - 0.700

的计算质心

我必须找到点和质心之间的欧氏距离。这就是我迷路的地方,因为我使用的欧氏距离公式是:

dist = \sqrt{\left ( x_{1}  - y_{1}\right )^2 + (x_2 - y_2)^2}

def __euclidean(self, x, y):
    return sqrt(sum(pow(a - b, 2) for a, b in zip(x, y)))

在这种情况下,x 变量是特征列表,y 变量是质心的值 - 0.7。 我是否遗漏了一些明显的东西?

编辑:我正在添加我正在尝试实现的算法以及质心的公式。

算法:

算法1的输入是训练数据,X = {x1, x2,...,xn},质心邻居数k,计算点间相似度的距离度量(Dist),以及将估计其 class 的目标点 p。输出是定义周围邻域的半径 目标点p.

质心公式:

def __compute_centroid(self, points):
    sum_centroid = []
    for p in points:
        sum_centroid.append(sum(p))
    return (1 / len(points)) * sum(sum_centroid)

我认为问题出在 compute_centroid 函数上。

对于笛卡尔坐标,质心位于独立于其他坐标计算的每个坐标的平均值处:

>>> import numpy as np
>>> points = [[1, 2, 3], [4, 5, 6]]
>>> centroid = np.mean(points, axis=0)
>>> centroid
array([2.5, 3.5, 4.5])

这是一个 3 向量,所以现在您可以计算它与另一个数据点之间的距离:

>>> q = np.array([7, 8, 9])
>>> np.linalg.norm(q - centroid)
7.794228634059948

(我认为你的欧几里得函数有效,但我没有测试它。)