General Numerical Python question

Tim Hochberg tim.hochberg at ieee.org
Tue Oct 14 12:31:55 EDT 2003


Michael Ressler wrote:
> 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 agree, you probably can't get rid of all the loops in this case, and 
if you could, the resulting code would probably be horrible. I have a 
couple of minor quibles with the above code though. I think I'd write it as:

lenavg =  len(a) - n + 1
avg  =np.zeros(lenavg, np.Float)
for i in range(n) : # window size to smooth over
     avg += a[i:lenavg+i]  # Using += reuses the same array every time
                      # Instead of creating a new one each time
                      # Through the loop.
avg /= n  # Same here.

The important point being the use of += and /=. And, in order to make 
that work, you need to set the type of avg appropriately, not let it 
default to int.

-tim


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





More information about the Python-list mailing list