[Numpy-discussion] finding minimum distance using arrays

wilson wilson.t.thompson at gmail.com
Wed Apr 23 02:00:56 EDT 2008


hi
i wrote a function to find euclidian distance between two vectors and
applied it to the rows of a 2d array of floats  as below


from math import sqrt
from numpy import array,sum

def distance(vec1, vec2):
    return sqrt(sum([(x-y)**2 for x,y in zip(vec1, vec2)]))

def findmatch(wts,inputwt):
    mindist=99.0
    index=0
    for i in range(len(wts)):
        d=distance(wts[i],inputwt)
        if d == 0.0:
            mindist=d
            index=i
            break
        elif d < mindist:
            mindist=d
            index=i
    return index,mindist

inputwt is a 1 dim array,wts is a 2d array .i want to find the
distance between inputwt and each row of wts...I used mindist=99.0  as
an improbable value for distance for  the sample float arrays which i
used to test it...
i am not sure if this is the proper way to write this function..can
someone suggest how i can make it better?



More information about the NumPy-Discussion mailing list