finding euclidean distance,better code?

D

devnew

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
 
G

Gabriel Genellina

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
 
G

Gabriel Genellina

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.
 
R

Robert Bossy

Gabriel said:
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
 
C

castironpi

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 uni-pole and/or lensed/focused/
parabolaed? Is that a yes-or-no question?
 
H

harryos

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
 
S

Steven D'Aprano

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.
 
R

Roel Schroeven

Steven D'Aprano schreef:
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
 
S

Steven D'Aprano

Steven D'Aprano schreef:

I'm not sure what your point is. The name

"The" name? You go on to list four additional names, so why do you say
that "Manhattan distance" is THE name? When I studied this at university,
we called it the taxi metric.

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.

Wikipedia doesn't believe that M-D is the primary or most common name,
and the link you give redirects to "Taxicab distance". Googlefight
agrees: "Taxicab distance" is more than twice as common, and "rectilinear
distance" more than five times as common.

My point was to draw attention to Robert's unconscious assumptions which
are reflected in his choice of language. Rectilinear distance applies to
more than "distance from one point to another in THIS city" (emphasis
added).

It applies in parts of Rome, Sydney, London, Moscow and many other
places. It even applies to sleepy little country towns like Bendigo and
Mildura here in Australia. Manhattan is hardly the only place where
cities are carved up into rectangular or square city blocks, and I doubt
that it applies to the entirety of Manhattan. It also applies to
supermarket isles, church pews, chess boards, pixels on a monitor and no
doubt other places as well.

The very name is New York-centric, just as much as if the English called
the science of acoustics "Big-Ben-onics" in reference to the peals of Big
Ben's clock. I had thought I had pointed that out with a little gentle
understatement.
 
R

Roel Schroeven

Steven D'Aprano schreef:
"The" name? You go on to list four additional names, so why do you say
that "Manhattan distance" is THE name? When I studied this at university,
we called it the taxi metric.



Wikipedia doesn't believe that M-D is the primary or most common name,
and the link you give redirects to "Taxicab distance". Googlefight
agrees: "Taxicab distance" is more than twice as common, and "rectilinear
distance" more than five times as common.

My point was to draw attention to Robert's unconscious assumptions which
are reflected in his choice of language. Rectilinear distance applies to
more than "distance from one point to another in THIS city" (emphasis
added).

You can hardly blame that on Robert. It's true that Manhattan distance
is not the only name and not even the most popular one, but it's also
true that it's a valid name, and that Robert didn't invent it, he merely
used an existing name.
It applies in parts of Rome, Sydney, London, Moscow and many other
places. It even applies to sleepy little country towns like Bendigo and
Mildura here in Australia. Manhattan is hardly the only place where
cities are carved up into rectangular or square city blocks, and I doubt
that it applies to the entirety of Manhattan.

No, but it's actually very close.

I just looked at the places you mention in Google Earth; while they do
have sections with rectangular layouts, in none of them it is as
prevalent and obvious as in Manhattan (Manhattan isn't a city of course
like the other ones, it's only a part of New York).

Now of course it's true that there are many other places with checker
board layouts, but I still don't think that makes Manhattan distance a
bad name.
The very name is New York-centric, just as much as if the English called
the science of acoustics "Big-Ben-onics" in reference to the peals of Big
Ben's clock. I had thought I had pointed that out with a little gentle
understatement.

I'm absolutely not USA/America/New York centric myself, but Manhattan
simply is a good and well-known example of a checker board layout.

There are worse examples of bad names: 'French fries' for example is
French-centric, but that's not the real problem. The real problem is
that the name is simply wrong because fries are Belgian (which is
disputed, but I believe it since I'm from Belgium ;) ). But I'm still
not going the call them freedom fries or anything.

In any case, I replied because your reaction didn't feel all that gentle
to me; to be honest, it felt rather rude. I apologize for interpreting
the tone incorrectly.

--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov

Roel Schroeven
 
G

Gerard Flanagan

"The" name? You go on to list four additional names, so why do you say
that "Manhattan distance" is THE name? When I studied this at university,
we called it the taxi metric.


Wikipedia doesn't believe that M-D is the primary or most common name,
and the link you give redirects to "Taxicab distance". Googlefight
agrees: "Taxicab distance" is more than twice as common, and "rectilinear
distance" more than five times as common.

My point was to draw attention to Robert's unconscious assumptions which
are reflected in his choice of language. Rectilinear distance applies to
more than "distance from one point to another in THIS city" (emphasis
added).

It applies in parts of Rome, Sydney, London, Moscow and many other
places. It even applies to sleepy little country towns like Bendigo and
Mildura here in Australia. Manhattan is hardly the only place where
cities are carved up into rectangular or square city blocks, and I doubt
[...]

a metric by any other name...

http://news.bbc.co.uk/2/hi/uk_news/northern_ireland/2595215.stm
 
P

Peter Otten

Steven said:
Wikipedia doesn't believe that M-D is the primary or most common name,
and the link you give redirects to "Taxicab distance". Googlefight
agrees: "Taxicab distance" is more than twice as common, and "rectilinear
distance" more than five times as common.

Could it be that there are many documents containing both taxicab and
distance that have nothing to do with the "taxicab distance"? Searching
with quotes around the search term google advertises

about 936 results for "taxicab distance"
6930 for "rectilinear distance"
38200 for "manhattan distance"
The very name is New York-centric, just as much as if the English called
the science of acoustics "Big-Ben-onics" in reference to the peals of Big
Ben's clock. I had thought I had pointed that out with a little gentle
understatement.

Whether you like a term and agree with its (alleged) bias or if it's in
common use are two different questions.

I think Robert addressed the latter while your answer implied the former.

Peter
 
S

Steven D'Aprano

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top