[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

eucledian dist calculations

nodrogbrown

1/21/2008 7:44:00 AM

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

3 Answers

John Machin

1/21/2008 9:16:00 AM

0

On Jan 21, 6:44 pm, nodrogbrown <nodrogbr...@gmail.com> wrote:
> 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

nodrogbrown

1/21/2008 4:08:00 PM

0


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

nodrogbrown

1/26/2008 6:18:00 AM

0

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