[英]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.