Answers for "moving average plt plot"

2

moving average numpy

def moving_average(a, n=3) :
    ret = np.cumsum(a, dtype=float)
    ret[n:] = ret[n:] - ret[:-n]
    return ret[n - 1:] / n

>>> a = np.arange(20)
>>> moving_average(a)
array([  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.,
        12.,  13.,  14.,  15.,  16.,  17.,  18.])
>>> moving_average(a, n=4)
array([  1.5,   2.5,   3.5,   4.5,   5.5,   6.5,   7.5,   8.5,   9.5,
        10.5,  11.5,  12.5,  13.5,  14.5,  15.5,  16.5,  17.5])
Posted by: Guest on November-21-2020
0

moving averages python

# option 1

df['data'].rolling(3).mean()
df['data'].shift(periods=1).rolling(3).mean()

# option 2: compute a 9-day simple moving average with pandas

BTC_USD['SMA_9'] = BTC_USD['Close'].rolling(window=9, min_periods=1).mean()
Posted by: Guest on October-25-2020

Python Answers by Framework

Browse Popular Code Answers by Language