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

查看:21
本文介绍了从 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}$

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

到目前为止我有这个:

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天全站免登陆