Moving Average

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Moving Average相关的知识,希望对你有一定的参考价值。

?????????[1]   mat   win   ror   exp   cep   day   plot   art   

??????????????????Demo

#!/usr/bin/python2.7
# Fetch data from BD and analyse.

import json
import urllib
import traceback
import numpy as np
# import pandas as pd
import matplotlib.pyplot as plt
#from scipy import stats

def fetch_raw_data(url):
    try:
        response = urllib.urlopen(url).read().decode(???utf-8???)
        return json.loads(response)
    except Exception, e:
        err = traceback.format_exc()
        print("fetch_raw_data err: {}".format(err))

# ??????????????????
def moving_average(f_t):
    if type(f_t) is not np.ndarray:
        raise TypeError            (???Expected one dimensional numpy array.???)
    if f_t.shape[1] != 1:
        raise IndexError            (???Expected one dimensional numpy array, %d dimensions given.??? % (f_t.shape[1]))

    f_t = f_t.flatten()
    window = 5
    mode = ???same???
    g_t = np.ones(int(window))/float(window)
    # Deal with boundaries with atleast lag/2 day window
    # ma = np.convolve(f_t,g_t,mode)
    # ma = np.convolve(f_t,g_t,mode)[window-1:-window+1]
    ma = np.convolve(f_t,g_t)[window-1:-window+1]
    return ma

def raw_data():
    start_ts = 1533204000
    stop_ts = 1533222000
    url = ???http://8.8.8.8/path/data?begin_time={}&end_time={}&type=asia???
    url = url.format(start_ts,stop_ts)
    result = fetch_raw_data(url)
    # downloadspeed_lst = result[???result???][???downloadspeed???]
    downloadspeed_lst = result[???result???][???totaluploadspeed???]
    downloadspeed_lst = [ [ele,] for ele in downloadspeed_lst ]
    return downloadspeed_lst

def run(downloadspeed_lst):
    downloadspeed_ndarray = np.array(downloadspeed_lst)
    ma = moving_average(downloadspeed_ndarray)
    return ma


data = raw_data()
ma = run(data)
t = np.arange(4, len(data))
plt.plot(t, data[4:], lw=1.0)
plt.plot(t, ma, lw=1.0)
plt.show()

 

???????????????

??????????????????

??????????????????????????????????????????????????????????????????????????????

 

?????????https://www.cnblogs.com/21207-iHome/p/6231607.html

 

以上是关于Moving Average的主要内容,如果未能解决你的问题,请参考以下文章

Moving Average from Data Stream

Moving Average from Data Stream

Moving Average from Data Stream

移动平均(moving average,MA)简单介绍

346. Moving Average from Data Stream - Easy

Leetcode 346. Moving Average from Data Stream