473,770 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ,selectedfacesp aces)
input_wk # of shape(selectedf acespaces,)
distance # of shape(selectedf acespaces,) initally all 0.0 's
mindistance #of shape(selectedf acespaces,) 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=dis tance.copy()
if sum(mindistance ) sum(distance):
imgindex=image
mindistance=dis tance.copy()
if max(mindistance ) 0.0:
#normalise mindistance
mindistance=min distance/(max(mindistanc e)+1)
dist=sum(mindis tance)

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 6378
En Mon, 17 Mar 2008 03:04:16 -0200, de****@gmail.co m <de****@gmail.c om>
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=dis tance.copy()
if sum(mindistance ) sum(distance):
imgindex=image
mindistance=dis tance.copy()
if max(mindistance ) 0.0:
#normalise mindistance
mindistance=min distance/(max(mindistanc e)+1)
dist=sum(mindis tance)

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(in put_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.a rwrote
>
_, imgindex = min((sum(abs(in put_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**********@g mail.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...@g mail.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.or g/wiki/Euclidean_dista nce
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**********@g mail.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
3904
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 are pre-calculated and passed to the query. SELECT * FROM Locations
6
4052
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 thesaurus appear at many places in my texts, but they are often misspelled, just slightly different from the thesaurus. Now I'm looking for the best strategy to match the appearence of my thesaurus items in the texts. Do I have to build patterns...
6
3779
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; strings.push_back("abc"); strings.push_back("xyz"); strings.push_back("lmnop");
20
17050
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. The others are free to go over ... (I already found that part) -> http://users.pandora.be/hebbrecht/jochen/c++/test.cpp
2
3309
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 partial match could be any of the following:- Partial match 1: 1 character maybe wrong Partial match 2: 2 characters maybe wrong Partial match 3: 3 characters maybe wrong
14
11335
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, e.g. f(x)=x^8+x^4+x^3+x+1 in C? How to represent the multiplication & division process of polynomial? Regards.
9
2324
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 list. For instance, if I did something like distance(list.end(), list.begin()), I would get -list.size(). The STL's iterator distance function amounts to something like this: distance_type distance(Iterator first, Iterator last) {...
5
2560
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 System/Known/Pre- defined colour. So they wont have corresponding names - this is a problem because my app needs to return the colour name of each pixel. So I have looked at GetNearestColor but this just seems to return the original color I passed it in the...
1
14206
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 algorithm from numpy import inf from copy import copy
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.