Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Euclidean distance with weights

I am currently using SciPy to calculate the euclidean distance

dis = scipy.spatial.distance.euclidean(A,B)

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?

What I have now: sqrt((a1-b1)^2 + (a2-b2)^2 +...+ (a5-b5)^2)

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.

Thanks

like image 463
Maggie Avatar asked Jan 14 '12 07:01

Maggie


1 Answers

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

enter image description here

then this should do the job:

def weightedL2(a,b,w):
    q = a-b
    return np.sqrt((w*q*q).sum())
like image 128
talonmies Avatar answered Sep 28 '22 18:09

talonmies