Answers for "python moving average"

3

python moving average time series

#Creating a 100 day moving average from 'df'
df.rolling(100).mean()
Posted by: Guest on September-08-2021
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
1

python moving average of list

import numpy
def running_mean(x, N):
  """ x == an array of data. N == number of samples per average """
    cumsum = numpy.cumsum(numpy.insert(x, 0, 0)) 
    return (cumsum[N:] - cumsum[:-N]) / float(N)
  
val = [-30.45, -2.65, 56.61, 47.13, 47.95, 30.45, 2.65, -28.31, -47.13, -95.89]
print(running_mean(val, 3))
""" [  7.83666667  33.69666667  50.56333333  41.84333333  27.01666667
   1.59666667 -24.26333333 -57.11      ]	"""
Posted by: Guest on June-15-2020
2

python plotting moving average

#Plot the moving average that is the result of the rolling window
r90 = data.rolling(window=90).mean() #<---- Moving Average
data.join(r90.add_suffix(' MA 90')).plot(figsize=(8,8))
plt.show()
Posted by: Guest on September-08-2021
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
0

12 month movinf average in python for dataframe

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
%matplotlib inline
import seaborn as sns
sns.set(style='darkgrid', context='talk', palette='Dark2')

my_year_month_fmt = mdates.DateFormatter('%m/%y')

data = pd.read_pickle('./data.pkl')
data.head(10)
Posted by: Guest on December-27-2020

Python Answers by Framework

Browse Popular Code Answers by Language