finding euclidean distance,better code?

devnew at gmail.com devnew at gmail.com
Mon Mar 17 06:04:16 CET 2008


hello
while trying to write a function that processes some numpy arrays and
calculate euclidean distance ,i ended up with this code
(though i used numpy ,i believe my problem has more to do with python
coding style..so am posting it here)

...
# i am using these numpy.ndarrays to do the calculation
facespace # of shape(totalimgs,imgpixels)
weights # of shape(totalimgs,selectedfacespaces)
input_wk # of shape(selectedfacespaces,)
distance # of shape(selectedfacespaces,) initally all 0.0 's
mindistance #of shape(selectedfacespaces,) initally all
0.0 's
...
...
#here is the calculations part

for image in range(numimgs):
    distance = abs(input_wk - weights[image, :])
    if image==0:
        #copy from distance to mindistance
        mindistance=distance.copy()
    if sum(mindistance) > sum(distance):
        imgindex=image
        mindistance=distance.copy()
if max(mindistance) > 0.0:
    #normalise mindistance
    mindistance=mindistance/(max(mindistance)+1)
dist=sum(mindistance)

this gets me the euclidean distance value.I want to know if the way i
coded it can be improved,made more compact....if someone can give
suggestions it will be a great help .
thanks
D



More information about the Python-list mailing list