欧几里得距离与权重 [英] Euclidean distance with weights
问题描述
我目前正在使用SciPy
来计算欧式距离
I am currently using SciPy
to calculate the euclidean distance
dis = scipy.spatial.distance.euclidean(A,B)
其中; A,B是5维位向量.现在可以正常使用,但是如果我为每个维度添加权重,是否仍可以使用scipy?
where; A, B are 5-dimension bit vectors. It works fine now, but if I add weights for each dimension then, is it still possible to use scipy?
我现在拥有的是:sqrt((a1-b1)^2 + (a2-b2)^2 +...+ (a5-b5)^2)
我想要的是:sqrt(w1(a1-b1)^2 + w2(a2-b2)^2 +...+ w5(a5-b5)^2)
使用scipy或numpy或任何其他有效的方式来做到这一点.
What I want: sqrt(w1(a1-b1)^2 + w2(a2-b2)^2 +...+ w5(a5-b5)^2)
using scipy or numpy or any other efficient way to do this.
谢谢
推荐答案
The suggestion of writing your own weighted L2 norm is a good one, but the calculation provided in this answer is incorrect. If the intention is to calculate
然后这应该可以完成工作:
then this should do the job:
def weightedL2(a,b,w):
q = a-b
return np.sqrt((w*q*q).sum())
这篇关于欧几里得距离与权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!