eucledian dist calculations

N

nodrogbrown

hi
i am using python to do some image data calculations..I use the
following numpy.ndarrays ,(i have given their shapes and ranks)

weights=ndarray :shape(100,30),ndim=2 will have vals like
2458121847.49 (of type 'numpy.float64')
input_weight=ndarray :shape(30,),ndim=1 (similar to above but diff
vals)
distance =ndarray :shape(30,),ndim=1
mindistance==ndarray :shape(30,),ndim=1

now i am calculating the euclidian distance of 'input_weight' from
'weight'
since this is the cumulative diff i do this in this way

<code>
for image in range(100):
temp=0.0
for j in range(30):
distance[j]=abs(input_weight[j]-weights[image,j])

if(image==0):
#at the start copy from distance to mindistance
mindistance=distance.copy()
if (sum(mindistance) > sum(distance)):
imgindex=image # i use this later to access a list
mindistance=distance.copy()

# now normalise the mindistance
array
if (max(mindistance) > 0.0):
mindistance=mindistance/(max(mindistance))

dist=sum(mindistance)

<code>

this gives me the correct results but i am worried if this is a bit
unpythonish?
(been a java programmer for a long time..) i wd like to know if there
is a better way

gordon
 
J

John Machin

hi
i am using python to do some image data calculations..I use the
following numpy.ndarrays ,(i have given their shapes and ranks)

weights=ndarray :shape(100,30),ndim=2 will have vals like
2458121847.49 (of type 'numpy.float64')
input_weight=ndarray :shape(30,),ndim=1 (similar to above but diff
vals)
distance =ndarray :shape(30,),ndim=1
mindistance==ndarray :shape(30,),ndim=1

now i am calculating the euclidian distance of 'input_weight' from
'weight'
since this is the cumulative diff i do this in this way

<code>
for image in range(100):
temp=0.0
for j in range(30):
distance[j]=abs(input_weight[j]-weights[image,j])

if(image==0):
#at the start copy from distance to mindistance
mindistance=distance.copy()
if (sum(mindistance) > sum(distance)):
imgindex=image # i use this later to access a list
mindistance=distance.copy()

# now normalise the mindistance
array
if (max(mindistance) > 0.0):
mindistance=mindistance/(max(mindistance))

dist=sum(mindistance)

<code>

this gives me the correct results

Are you sure? What happens if the vector with the smallest
sum(distance) is the first one?
but i am worried if this is a bit
unpythonish?
(been a java programmer for a long time..) i wd like to know if there
is a better way

1. 'temp' is not used
2. Lose the superfluous parentheses in 'if' statements
3. Put space around operators
4. I've never used any of numpy & friends, but:
(a) Can't you replace the inner loop with something like this:
distance = abs(input_weight - weights[image, :])
(b) I doubt that you need the .copy()
5. Lose the hard-wired numbers like 30 and 100
6. Put it inside a function and *TEST* it

The word you were looking for is 'unpythonic', but the principles
behind the above apply to any language.

HTH,
John
 
N

nodrogbrown

1. 'temp' is not used
2. Lose the superfluous parentheses in 'if' statements
3. Put space around operators
4. I've never used any of numpy & friends, but:
(a) Can't you replace the inner loop with something like this:
distance = abs(input_weight - weights[image, :])
(b) I doubt that you need the .copy()
5. Lose the hard-wired numbers like 30 and 100
6. Put it inside a function and *TEST* it

thanx John , will go thru those and do the cleanup
gordon
 
N

nodrogbrown

sorry..the last post didn't appear!
Are you sure? What happens if the vector with the smallest
sum(distance) is the first one?

initially i will set imgindex =0 so if the first one is of smallest
sum..,the image will be reteived by imgindex 0



(a) Can't you replace the inner loop with something like this:
distance = abs(input_weight - weights[image, :])

good suggestion ..was doing it java way!
(b) I doubt that you need the .copy()
5. Lose the hard-wired numbers like 30 and 100

those nums were only for clarification..
6. Put it inside a function and *TEST* it
will do...
thanx john ,your post really helped
gordon
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top