简体   繁体   English

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

[英]How to use moving average in python?

I want to make a time series prediction using simple moving average.我想使用简单的移动平均线进行时间序列预测。 I am using the below code:-我正在使用以下代码:-

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))

I am not able to understand how to predict next 10 values as y_hat is giving me only 1 value.我无法理解如何预测接下来的 10 个值,因为 y_hat 只给了我 1 个值。 Also y_hat is not matching with mean of data which should match as i am using order 1 in MA. y_hat 也与数据的平均值不匹配,因为我在 MA 中使用订单 1 时应该匹配。 Can anyone help me on this?谁可以帮我这个事?

y_hat=model_fit.predict(len(data),len(data))

your start value is len(data) and end value is also the same so it's giving you the single predicted value.您的起始值是 len(data) 并且结束值也相同,因此它为您提供了单个预测值。

I used forecast() in my project.我在我的项目中使用了预测()。 Here is my code snippet:这是我的代码片段:

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()

在此处输入图像描述

Forecasting value is constant in my graph because my data has seasonal components.我的图表中的预测值是恒定的,因为我的数据具有季节性成分。

I think you just have to give the start and end value like this我认为你只需要像这样给出开始和结束值

 model_fit.predict(0,10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM