Python實(shí)現(xiàn)滑動(dòng)平均(Moving Average)的例子
更新時(shí)間:2019年08月24日 17:13:51 作者:Luke__Zhang
今天小編就為大家分享一篇Python實(shí)現(xiàn)滑動(dòng)平均(Moving Average)的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
Python中滑動(dòng)平均算法(Moving Average)方案:
#!/usr/bin/env python # -*- coding: utf-8 -*- import numpy as np # 等同于MATLAB中的smooth函數(shù),但是平滑窗口必須為奇數(shù)。 # yy = smooth(y) smooths the data in the column vector y .. # The first few elements of yy are given by # 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 # ... def smooth(a,WSZ): # a:原始數(shù)據(jù),NumPy 1-D array containing the data to be smoothed # 必須是1-D的,如果不是,請(qǐng)使用 np.ravel()或者np.squeeze()轉(zhuǎn)化 # WSZ: smoothing window size needs, which must be odd number, # as in the original MATLAB implementation out0 = np.convolve(a,np.ones(WSZ,dtype=int),'valid')/WSZ r = np.arange(1,WSZ-1,2) start = np.cumsum(a[:WSZ-1])[::2]/r stop = (np.cumsum(a[:-WSZ:-1])[::2]/r)[::-1] return np.concatenate(( start , out0, stop )) # another one,邊緣處理的不好 """ def movingaverage(data, window_size): window = np.ones(int(window_size))/float(window_size) return np.convolve(data, window, 'same') """ # another one,速度更快 # 輸出結(jié)果 不與原始數(shù)據(jù)等長(zhǎng),假設(shè)原數(shù)據(jù)為m,平滑步長(zhǎng)為t,則輸出數(shù)據(jù)為m-t+1 """ def movingaverage(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實(shí)現(xiàn)滑動(dòng)平均(Moving Average)的例子就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python利用numpy實(shí)現(xiàn)三層神經(jīng)網(wǎng)絡(luò)的示例代碼
這篇文章主要介紹了Python利用numpy實(shí)現(xiàn)三層神經(jīng)網(wǎng)絡(luò)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Python?設(shè)計(jì)模式行為型訪問(wèn)者模式
這篇文章主要介紹了Python?設(shè)計(jì)模式行為型訪問(wèn)者模式,訪問(wèn)者模式即Visitor?Pattern,訪問(wèn)者模式,指作用于一個(gè)對(duì)象結(jié)構(gòu)體上的元素的操作,下文相關(guān)資料需要的小伙伴可以參考一下2022-02-02Python學(xué)習(xí)筆記之Python的下載、腳本與交互模式、注釋
這篇文章主要介紹了Python學(xué)習(xí)筆記之Python的下載、腳本與交互模式、注釋,本文從基礎(chǔ)開始學(xué)習(xí)Python,需要的朋友可以參考下2023-03-03PyChon中關(guān)于Jekins的詳細(xì)安裝(推薦)
這篇文章主要介紹了PyChon中關(guān)于Jekins的詳細(xì)安裝(推薦),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12