首页 > 编程知识 正文

python为什么叫爬虫,python怎么求列表的平均值

时间:2023-05-06 04:59:35 阅读:141990 作者:4030

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实现“滑动平均”的例子,就是编辑分享给大家的所有内容。 希望您能参考。 另外,我想支持脚本屋。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。