John Machin
1/21/2008 9:16:00 AM
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