Python中的滑动平均算法(Moving Average )方案:
#! /usr/敏感串行/env python
#-* -编码: utf-8-* -
导入编号为NP
与MATLAB中的smooth函数相同,但平滑窗口必须为奇数。
#YY=smooth(y ) smoothsthedatainthecolumnvectory .
# thefirstfewelementsofyyaregivenby
#YY(1)=y(1) ) ) ) ) ) ) ) )。
#YY(2)=) y )1) y )2) y )3)/3
#YY(3)=) y )1) y )2) y )3) y )4) y )5)/5
#YY(4)=) y )2) y )3) y )4) y )5) y )6)/5
# .
defsmooth(a,WSZ ) :
# a:原始数据,numpy1- darraycontainingthedatatobesmoothed
#必须是1-D。 否则,请使用np.ravel (或np.squeeze )转换
# wsz : smoothingwindowsizeneeds,which must be odd number,
# asintheoriginalmatlabimplementation
out0=NP.convolve(a,NP.ones ) wsz,dtype=int ),' valid ' )/WSZ
r=NP.arange(1,wsz-1,2 ) ) ) ) )。
start=NP.cumsum(a[:wsz-1] ) 336033602 )/r
stop=(NP.cumsum (a [ :-wsz :-1 ] ) 3360:2 )/r ) [:-1]
returnNP.concatenate((start,out0,stop ) )
# another one,边缘处理不好
“”'
defmovingaverage(data,window_size ) :
window=NP.ones(int(window_size ) )/float (window _ size ) )。
returnNP.convolve(data,window,' same ' ) )。
“”'
# another one,快一点
#输出结果与原始数据的长度不同,如果原始数据为m,平滑步为t,则输出数据为m-t 1
“”'
defmovingaverage(data,window_size ) :
cumsum _ vec=NP.cumsum (NP.insert (data,0,0 ) )
ma _ vec=(cumsum _ vec [ window _ size : ]-cumsum _ vec [ :-window _ size ] )/window_size
return ma_vec
“”'
以上Python实现“滑动平均”的例子,就是编辑分享给大家的所有内容。 希望您能参考。 另外,我想支持脚本屋。