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  
Share this Question
P: n/a

En Mon, 17 Mar 2008 03:04:16 0200, de****@gmail.com <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 1norm
(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  
P: n/a

On Mar 17, 6:17 pm, "Gabriel Genellina" <gagsl...@yahoo.com.arwrote
>
_, 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  
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  
P: n/a

En Fri, 28 Mar 2008 12:15:48 0300, harryos <os**********@gmail.com>
escribió:
>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?
(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 norm1, or L1; the usual
euclidean distance is derived from norm2.
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  
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  
P: n/a

On Mar 28, 10:15*am, harryos <oswald.ha...@gmail.comwrote:
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
wikipediahttp://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?
I want the angle into an array. < sign bit on distance to [zero
element/kernel]. Are you coming out in unipole and/or lensed/focused/
parabolaed? Is that a yesorno question?  
P: n/a

the norm from which it is derived is called norm1, or L1; the usual euclidean distance is derived from norm2.
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  
P: n/a

En Fri, 28 Mar 2008 18:15:04 0300, harryos <os**********@gmail.com>
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  
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 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.
You know, there are other cities than Manhattan. Some of them even have
streets and blocks.

Steven  
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 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.
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  
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   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 5791
 replies: 11
 date asked: Mar 17 '08
