473,395 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

finding euclidean distance,better code?

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 6347
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 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
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
Mar 18 '08 #3
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
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 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
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
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 uni-pole and/or lensed/focused/
parabolaed? Is that a yes-or-no question?
Mar 28 '08 #7
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
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

Mar 28 '08 #9
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
Mar 29 '08 #10
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
Mar 29 '08 #11
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Xenophobe | last post by:
I have successfully converted the ASP code included in the following article to PHP: http://www.4guysfromrolla.com/webtech/040100-1.shtml As described the high and low latitudes and longitudes...
6
by: Christoph Pingel | last post by:
Hi all, an interesting problem for regex nerds. I've got a thesaurus of some hundred words and a moderately large dataset of about 1 million words in some thousand small texts. Words from the...
6
by: Joe | last post by:
I have a: vector<string> which contains a few dozen elements. I want to find the index of the element containing a certain string. for example: vector<string> strings;...
20
by: Webdad | last post by:
Hi! I running my first year as industrial engineer (informatics) We have an assignment to do : .... create a playfield (matrix). Some places in that field are blocked, so you can't pass them....
2
by: B Moor | last post by:
I have a database with 100,000's records, each with a unique reference, eg A123BNK456 I would like to generate a search facility whereby we can choose an exact match or partial match, where the...
14
by: Tiza Naziri | last post by:
Hi, Anybody have an idea on how to start writing a C code for generating the inverse of finite field GF(2^8) using extended Euclidean algorithm? What I mean is how to represent a polynomial,...
9
by: nottheartistinquestion | last post by:
As an intellectual exercise, I've implemented an STL-esque List<and List<>::Iterator. Now, I would like a signed distance between two iterators which corresponds to their relative position in the...
5
by: pepito72 | last post by:
Hi A problem with trying to find the nearest colour. I have an image which I use GetPixel to return the colour of various points. However .... not all colours returned will be a...
1
by: Glenton | last post by:
Hi All Here is a very simple little class for finding a shortest route on a network, following Dijkstra's Algorithm: #!/usr/bin/env python #This is meant to solve a maze with Dijkstra's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.