Python中数据点的平均趋势曲线 [英] Average trend curve for data points in Python
问题描述
我很想重现与此类似的情节:
(来源: brleader.com ) >
我的意思是我有一组数据点,我希望有一条曲线可以显示平均趋势.
我尝试将随机噪声添加到函数y = 2x
from scipy import interpolate
x=arange(0,1,1e-3)
noise=np.random.random(len(x))
y=2*x+noise
然后我使用了一些Scipt函数来插值数据
xnew=arange(0,1,1e-1)
f=interpolate.UnivariateSpline(x,y)
g=interpolate.interp1d(x,y)
plot(x,y,'ro',xnew,f(xnew),'-',xnew,g(xnew),'--')
show()
但是我得到的曲线几乎不像y = 2 * x.我希望有一条平滑的曲线可以平均数据.我可以使用哪种方法/功能?
曲线看起来不像y=2*x
的原因之一(我认为确实如此,但这要视情况而定)是您的噪音很大与y的平均变化相比.如果您尝试以下操作:
noise=0.1*np.random.random(len(x))
(即降低噪音)或
y=5*x**2+noise
(即,将y
中的更改放大),您会看到插值可以更好地跟踪数据.
您可能还想签出:
http://www.scipy.org/Cookbook/SignalSmooth
I'd love to reproduce a plot similar to this:
(source: brleader.com)
I mean I have a set of data points and I'd love to have a curve which shows the average trend.
I tried adding random noise to the function y=2x
from scipy import interpolate
x=arange(0,1,1e-3)
noise=np.random.random(len(x))
y=2*x+noise
And then I used some of the Scipt function to interpolate data
xnew=arange(0,1,1e-1)
f=interpolate.UnivariateSpline(x,y)
g=interpolate.interp1d(x,y)
plot(x,y,'ro',xnew,f(xnew),'-',xnew,g(xnew),'--')
show()
But the curve I get hardly resemble y=2*x. I'd love to have a smooth curve which average the data. Which method/function can I use?
One of the reasons why the curve doesn't look like y=2*x
(I think it does, but that's subject to opinion) is that your noise is large compared to the average change in y. If you try something like:
noise=0.1*np.random.random(len(x))
(i.e make the noise smaller) or
y=5*x**2+noise
(i.e make the change in y
larger), you'll see that the interpolation tracks the data better.
You might also want to check out:
http://www.scipy.org/Cookbook/SignalSmooth
这篇关于Python中数据点的平均趋势曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!