General Numerical Python question

Michael Ressler ressler at cheetah.jpl.nasa.gov
Tue Oct 14 10:56:35 EDT 2003


In article <500a4565.0310140541.4f4fd35f at posting.google.com>, 2mc wrote:
> May I ask you for a little more help.  The example you gave was very
> good and it was something I hadn't thought ot.  However, I need the 25
> row "window" to move through the entire array one row at a time.  In
> other words each 25 row 'chunk' of data will contain 24 rows of the
> previous 'chunk'.  Unless I misunderstood your code, each 'chunk' has
> a unique set of rows - there is no overlapping.
> 
> Do you have any ideas how I could do this without loops?

Okay, maybe you can't get rid of all loops as I implied in my previous
"loops are evil" post, but the trick is to minimize them.

This is "pseudo-code", so don't try to run it, but see if the ideas
are useful. One way to approach "running" things, is extensive use of
subarrays. Suppose you want to do a running average on an n-element
run of a large array a with m elements (let's ignore the endpoints for
now). This isn't the best way to do a running average, but it might
help with things more complex than an average.

m=len(a)
avg=zeros(len(a))
for i in range(n) : # window size to smooth over
    avg=avg+a[i:m-n+i]
avg=avg/n

I think I might have screwed up the syntax of the subarray statement
(I'm still much better with the commercial language IDL than I am with
Numeric, and I get them confused, but the thought process is the
same). The idea is to pile up subarrays which have been shifted by
one. Instead of a zillion loops through the array, you only have to
deal with n (in your case 25) cycles.

This is what I meant by "clever expressions" in my first response.
Hope this stimulates more ideas.

Mike

-- 
Dr. Michael Ressler
Research Scientist, Astrophysics Research Element, Jet Propulsion Laboratory
Email: ressler at cheetah.jpl.nasa.gov      Phone: (818)354-5576
"A bad night at the telescope is still better than the best day at the office."




More information about the Python-list mailing list