简体   繁体   English

计算 python 中所有点到给定点的加权距离

[英]Calculate weighted distance from all points to a given point in python

I'm trying to calculate the weighted distance from a set of points to a given point.我正在尝试计算从一组点到给定点的加权距离。 The weighted distance function looks like the following.加权距离 function 如下所示。

在此处输入图像描述

The set of points (X in the code) looks like the following点集(代码中的 X)如下所示

[[-14.78816795   1.        ]
 [-13.39187241   1.        ]
 [ -6.58691072   1.        ]
 ...
 [ -3.45446849  99.        ]
 [ 30.75222397  99.        ]
 [ 32.22760391  99.        ]]

and my given points (Centroids in the code) looks like the following我给定的点(代码中的质心)如下所示

array([[ -0.75675046, -28.46644783,   0.76348782],
       [ 80.        ,  74.        ,  83.        ]])

for unweighted Euclidian Distance i understand that i can use the following code对于未加权的欧几里得距离,我知道我可以使用以下代码

k=3
for k in range(K):
       tempDist=np.sum((X-Centroids[:,k])**2,axis=1)
       EuclidianDistance=np.c_[EuclidianDistance,tempDist]

but i'm not very sure about how to apply the weight to the x and y components.但我不太确定如何将权重应用于 x 和 y 分量。

First of all I think you have a couple of mistakes in your unweighted Euclidian Distance.首先,我认为您的未加权欧几里得距离有几个错误。 There seems to be lacking a squareroot and K is undefined.似乎缺少平方根,并且 K 未定义。

I have included the weights while trying to remain faithfully to the code you already had.我已经包含了权重,同时试图忠实于你已经拥有的代码。

greek_lambda=0.2
W=np.array([[greek_lambda,0],[0,1-greek_lambda]])

for k in range(3):
   tempDist=np.sqrt(np.sum(np.dot((X-Centroids[:,k]),W)**2,axis=1))
   EuclidianDistance=np.c_[EuclidianDistance,tempDist]
print(EuclidianDistance)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据索引计算numpy数组中所有点到单个点的距离 - calculate distance from all points in numpy array to a single point on the basis of index 线到给定点的距离(给定线的起点和终点) - Distance from line to given point (given start and end points for line) 计算从一个点到另一个点的距离 - Calculate distance from one point to all others 是否有 Python function 可以遍历数据集并计算距给定初始点的欧氏距离? - Is there a Python function that can iterate through a dataset and calculate the Euclidean distance from a given initial point? 在 Python 中计算加权成对距离矩阵 - Calculate weighted pairwise distance matrix in Python 替代(python)计算两个不同集合中所有点之间的距离 - Alternative (python) to calculate distance between all points at two different sets 找到数据集中所有点的最近距离点 - Python - Find the nearest point in distance for all the points in the dataset - Python 如何找到一个点在特定距离内的所有相邻点? - How to find all the neighboring points of a point within particular distance from it? 如何按到给定点的距离对点列表进行排序? - How to order a list of points by distance to a given point? 如何使用python计算数据点与聚类中心的距离 - how to calculate distance of data points from cluster centers using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM