从Python中的数据点查找移动平均值 [英] Finding moving average from data points in Python

查看:212
本文介绍了从Python中的数据点查找移动平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我再次使用Python进行游戏,我发现了一本整齐的例子.示例之一是绘制一些数据.我有两列的.txt文件,并且有数据.我对数据进行了很好的绘制,但在练习中它说:进一步修改程序以计算和绘制数据的运行平均值,定义如下:

I am playing in Python a bit again, and I found a neat book with examples. One of the examples is to plot some data. I have a .txt file with two columns and I have the data. I plotted the data just fine, but in the exercise it says: Modify your program further to calculate and plot the running average of the data, defined by:

$Y_k=\frac{1}{2r}\sum_{m=-r}^r y_{k+m}$

在这种情况下,其中r=5(y_k是数据文件中的第二列).让程序在同一张图上同时绘制原始数据和移动平均值.

where r=5 in this case (and the y_k is the second column in the data file). Have the program plot both the original data and the running average on the same graph.

到目前为止,我有这个:

So far I have this:

from pylab import plot, ylim, xlim, show, xlabel, ylabel
from numpy import linspace, loadtxt

data = loadtxt("sunspots.txt", float)
r=5.0

x = data[:,0]
y = data[:,1]

plot(x,y)
xlim(0,1000)
xlabel("Months since Jan 1749.")
ylabel("No. of Sun spots")
show()

那么我该如何计算总和?在Mathematica中,它很简单,因为它是符号操作(例如Sum [i,{i,0,10}]),但是如何在python中计算求和,求和取数据中的每十个点并将其平均,直到最后点?

So how do I calculate the sum? In Mathematica it's simple since it's symbolic manipulation (Sum[i, {i,0,10}] for example), but how to calculate sum in python which takes every ten points in the data and averages it, and does so until the end of points?

我看了看这本书,但没有发现任何可以解释这个的内容:\

I looked at the book, but found nothing that would explain this :\

heltonbiker的代码成功完成了^^:D

heltonbiker's code did the trick ^^ :D

from __future__ import division
from pylab import plot, ylim, xlim, show, xlabel, ylabel, grid
from numpy import linspace, loadtxt, ones, convolve
import numpy as numpy

data = loadtxt("sunspots.txt", float)

def movingaverage(interval, window_size):
    window= numpy.ones(int(window_size))/float(window_size)
    return numpy.convolve(interval, window, 'same')

x = data[:,0]
y = data[:,1]


plot(x,y,"k.")
y_av = movingaverage(y, 10)
plot(x, y_av,"r")
xlim(0,1000)
xlabel("Months since Jan 1749.")
ylabel("No. of Sun spots")
grid(True)
show()

我明白了:

非常感谢^^:)

推荐答案

在阅读此答案之前,请记住,下面还有另一个来自Roman Kh的答案,该答案使用numpy.cumsum并且比该答案要快得多.

Before reading this answer, bear in mind that there is another answer below, from Roman Kh, which uses numpy.cumsum and is MUCH MUCH FASTER than this one.


Best 将移动/滑动平均值(或任何其他滑动窗口函数)应用于信号的一种常见方法是使用numpy.convolve().


Best One common way to apply a moving/sliding average (or any other sliding window function) to a signal is by using numpy.convolve().

def movingaverage(interval, window_size):
    window = numpy.ones(int(window_size))/float(window_size)
    return numpy.convolve(interval, window, 'same')

在这里,interval是您的x数组,而window_size是要考虑的样本数.该窗口将以每个样本为中心,因此它将在当前样本之前和之后获取样本,以计算平均值.您的代码将变为:

Here, interval is your x array, and window_size is the number of samples to consider. The window will be centered on each sample, so it takes samples before and after the current sample in order to calculate the average. Your code would become:

plot(x,y)
xlim(0,1000)

x_av = movingaverage(interval, r)
plot(x_av, y)

xlabel("Months since Jan 1749.")
ylabel("No. of Sun spots")
show()

希望这会有所帮助!

这篇关于从Python中的数据点查找移动平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆