[Numpy-discussion] improving code

devnew@gmai... devnew@gmai...
Sun Mar 16 10:25:54 CDT 2008


hello
while trying to write a function that processes some numpy arrays and
calculate euclidean distance ,i ended up with this code


#some samplevalues
totalimgs=17
selectedfacespaces=6
imgpixels=18750 (ie for an image of 125X150 )
...
# i am using these arrays to do the calculation
facespace #numpy.ndarray of shape(totalimgs,imgpixels)
weights #numpy.ndarray of shape(totalimgs,selectedfacespaces)
input_wk #numpy.ndarray of shape(selectedfacespaces,)
distance #numpy.ndarray of shape(selectedfacespaces,) initally all 0.0
's
mindistance #numpy.ndarray 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:
    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 would be nice
thanks
D


More information about the Numpy-discussion mailing list