如何在python中使用移动平均?

如何在python中使用移动平均?,python,time-series,moving-average,Python,Time Series,Moving Average,我想用简单的移动平均做一个时间序列预测。我正在使用以下代码:- from statsmodels.tsa.arima_model import ARMA import statistics data=[x + random() for x in range(1,100)] model=ARMA(data,order=(0,1)) model_fit=model.fit(disp=False) y_hat=model_fit.predict(len(data),len(data)) 我无法理解如

我想用简单的移动平均做一个时间序列预测。我正在使用以下代码:-

from statsmodels.tsa.arima_model import ARMA
import statistics
data=[x + random() for x in range(1,100)]
model=ARMA(data,order=(0,1))
model_fit=model.fit(disp=False)
y_hat=model_fit.predict(len(data),len(data))
我无法理解如何预测接下来的10个值,因为y_只能给我1个值。此外,y_hat与数据的平均值不匹配,因为我在MA中使用的是顺序1。有人能帮我吗

y_hat=model_fit.predict(len(data),len(data))
您的起始值是lendata,而结束值也是相同的,因此它为您提供了单个预测值

我在我的项目中使用了预测。以下是我的代码片段:

from statsmodels.tsa.stattools import acf

# Create Training and Test
train = df[:3000]
test = df[3000:]

# Build Model
# model = ARIMA(train, order=(3,2,1))  
model = ARIMA(train, order=(1, 1, 1))  
fitted = model.fit(disp=-1)  

# Forecast
fc, se, conf = fitted.forecast(len(test), alpha=0.05)  # 95% conf

# Make as pandas series
fc_series = pd.Series(fc, index=test.index)
lower_series = pd.Series(conf[:, 0], index=test.index)
upper_series = pd.Series(conf[:, 1], index=test.index)

# Plot
plt.figure(figsize=(12,5), dpi=100)
plt.plot(train, label='training')
plt.plot(test, label='actual')
plt.plot(fc_series, label='forecast')
plt.fill_between(lower_series.index, lower_series, upper_series, 
                 color='k', alpha=.15)
plt.title('Forecast vs Actuals')
plt.legend(loc='upper left', fontsize=8)
plt.show()

预测值在我的图表中是恒定的,因为我的数据有季节性成分。

我想你只需要像这样给出起始值和结束值

 model_fit.predict(0,10)