425,478 Members | 1,833 Online Need help? Post your question and get tips & solutions from a community of 425,478 IT Pros & Developers. It's quick & easy.

# finding euclidean distance,better code?

 P: n/a 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 Mar 17 '08 #1
11 Replies

 P: n/a En Mon, 17 Mar 2008 03:04:16 -0200, de****@gmail.com escribiï¿½: 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) 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. It looks like you're rather computing a distance derived from the 1-norm (sum of coordinates; like in a city with square blocks). I'd save the sum(mindistance) value to avoid recomputing it in every iteration. 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 . The code is pretty legible as it is now. Anyway, using min() and a generator: _, imgindex = min((sum(abs(input_wk - weights[image, :])),image) for image in xrange(numimgs)) mindistance = abs(input_wk - weights[imgindex, :]) # normalize and sum again -- Gabriel Genellina Mar 17 '08 #2

 P: n/a On Mar 17, 6:17 pm, "Gabriel Genellina" _, imgindex = min((sum(abs(input_wk - weights[image, :])),image) for image in xrange(numimgs)) mindistance = abs(input_wk - weights[imgindex, :]) # normalize and sum again thanks Gabriel D Mar 18 '08 #3

 P: n/a The code is pretty legible as it is now. Anyway, using min() and a generator: hi is this calculated distance really Euclidean distance? When i checked wikipedia http://en.wikipedia.org/wiki/Euclidean_distance it shows a calculation involving sum of squares of the differences of elements.Here in this code ,the sum of coordinates are used? is that a different measure? oharry Mar 28 '08 #4

 P: n/a En Fri, 28 Mar 2008 12:15:48 -0300, harryos escribió: >The code is pretty legible as it is now. Anyway, using min() and agenerator: hi is this calculated distance really Euclidean distance? When i checked wikipedia http://en.wikipedia.org/wiki/Euclidean_distance it shows a calculation involving sum of squares of the differences of elements.Here in this code ,the sum of coordinates are used? is that a different measure? (Thanks for trimming the irrelevant parts of the message, that's good; but you trimmed too much text, even attribution lines - the above quoted sentence was mine) That's what I said in another paragraph. "sum of coordinates" is using a different distance definition; it's the way you measure distance in a city with square blocks. I don't know if the distance itself has a name, but the norm from which it is derived is called norm-1, or L1; the usual euclidean distance is derived from norm-2. See http://mathworld.wolfram.com/VectorNorm.html If you only want to see if two things are "close enough", this provides a faster measure than the euclidean distance. -- Gabriel Genellina Mar 28 '08 #5

 P: n/a Gabriel Genellina wrote: That's what I said in another paragraph. "sum of coordinates" is using a different distance definition; it's the way you measure distance in a city with square blocks. I don't know if the distance itself has a name, but I think it is called Manhattan distance in reference of the walking distance from one point to another in this city. RB Mar 28 '08 #6

 P: n/a On Mar 28, 10:15*am, harryos

 P: n/a the norm from which it is derived is called norm-1, or L1; the usual euclidean distance is derived from norm-2. If you only want to see if two things are "close enough", this provides a faster measure than the euclidean distance. thanks Gabriel for the detailed explanation.. if i were to calculate the euclidean distance in the above example how should i go about it..? should i replace distance = abs(input_wk - weights[image, :]) with something else? thanks again oharry Mar 28 '08 #8

 P: n/a En Fri, 28 Mar 2008 18:15:04 -0300, harryos escribió: if i were to calculate the euclidean distance in the above example how should i go about it..? should i replace distance = abs(input_wk - weights[image, :]) with something else? For a single 2D vector, math.hypot does what you want. But the above looks like a NumPy array; see this thread http://projects.scipy.org/pipermail/...il/027166.html -- Gabriel Genellina Mar 28 '08 #9

 P: n/a On Fri, 28 Mar 2008 16:59:59 +0100, Robert Bossy wrote: Gabriel Genellina wrote: >That's what I said in another paragraph. "sum of coordinates" is usinga different distance definition; it's the way you measure distance in acity with square blocks. I don't know if the distance itself has aname, but I think it is called Manhattan distance in reference of the walking distance from one point to another in this city. You know, there are other cities than Manhattan. Some of them even have streets and blocks. -- Steven Mar 29 '08 #10

 P: n/a Steven D'Aprano schreef: On Fri, 28 Mar 2008 16:59:59 +0100, Robert Bossy wrote: >Gabriel Genellina wrote: >>That's what I said in another paragraph. "sum of coordinates" is usinga different distance definition; it's the way you measure distance in acity with square blocks. I don't know if the distance itself has aname, but I think it is called Manhattan distance in reference of the walkingdistance from one point to another in this city. You know, there are other cities than Manhattan. Some of them even have streets and blocks. I'm not sure what your point is. The name of the distance happens to be Manhattan distance (or taxicab distance, rectilinear distance, L1 distance, city block distance; see http://en.wikipedia.org/wiki/Manhattan_distance) so Robert has a valid point. -- The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom. -- Isaac Asimov Roel Schroeven Mar 29 '08 #11

 P: n/a On Sat, 29 Mar 2008 13:06:27 +0100, Roel Schroeven wrote: In any case, I replied because your reaction didn't feel all that gentle to me; to be honest, it felt rather rude. Are you new to Usenet? :-) No offense taken; I hope Robert didn't take offense either, but took the little dig in the spirit it was intended. -- Steven Mar 29 '08 #12

### This discussion thread is closed

Replies have been disabled for this discussion. 