Python numpy moving average for data

February 24, 2011 at 11:58 pm 5 comments

The following examples produces a moving average of the preceding WINDOW values. We truncate the first (WINDOW -1) values since we can’t find the average before them. (The default behaviour for convolution is to assume that values before the start of our sequence are 0). (More formally, we construct the sequence y for the sequence x where y_i = (x_i + x_(i+1) + …. x_(i+n)) / n)

WINDOW = 10
data = [1,2,3,4,5,5,5,5,5,5,5,5,5,5,5]
weightings = numpy.repeat(1.0, WINDOW) / WINDOW
numpy.convolve(data, weightings)[WINDOW-1:-(WINDOW-1)]

This makes use of numpy’s convolution function. This is a general purpose moving average operation.

Changing weightings makes some values more important; offsetting appropriately allows you to view average as around point rather than before point.

Rather than truncating values we can fix the initial values in place, as illustrated in this example:

WINDOW = 10
data = [1,2,3,4,5,5,5,5,5,5,5,5,5,5,5]
extended_data = numpy.hstack([[data[0]] * (WINDOW- 1), data])
weightings = numpy.repeat(1.0, WINDOW) / WINDOW
numpy.convolve(extended_data, weightings)[WINDOW-1:-(WINDOW-1)]
About these ads

Entry filed under: Uncategorized. Tags: , , .

Setting the minimum values for axes in matplotlib Perl one-liners

5 Comments Add your own

  • 1. Christian  |  April 26, 2011 at 4:47 pm

    Thanks for the tip, I found it useful!

    You have a slight error in your fixed initial value example: “extended_data” should be the one being convolved, not “data”.

    Reply
    • 2. Anon  |  April 26, 2011 at 6:20 pm

      Thanks for spotting that! I’ve amended the example.

      Reply
  • 3. Greg Haskins  |  June 16, 2011 at 12:03 am

    Nice tip, thanks. I knew there had to be an optimized way for numpy to compute rolling averages.

    From the docs (http://docs.scipy.org/doc/numpy/reference/generated/numpy.convolve.html), it looks like your recipe could be even more concise by using the mode=”valid” keyword instead of slicing:

    >>> WINDOW = 10
    >>> data = [1,2,3,4,5,5,5,5,5,5,5,5,5,5,5]
    >>> weightings = numpy.repeat(1.0, WINDOW) / WINDOW
    >>> numpy.convolve(data, weightings)[WINDOW-1:-(WINDOW-1)]
    array([ 4. , 4.4, 4.7, 4.9, 5. , 5. ])
    >>> numpy.convolve(data, weightings, ‘valid’)
    array([ 4. , 4.4, 4.7, 4.9, 5. , 5. ])

    Reply
  • 4. Simple moving average and the swiss franc « quantathoughts  |  September 11, 2011 at 4:37 pm

    [...] to this post and comments for the [...]

    Reply
  • 5. daFeda  |  September 11, 2012 at 10:36 am

    Very useful, thank you.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


February 2011
M T W T F S S
« Dec   Mar »
 123456
78910111213
14151617181920
21222324252627
28  

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: