
上QQ阅读APP看书,第一时间看更新
How to do it...
In the following steps, we demonstrate several methods for making predictions using time series data:
- Begin by generating a time series:
from random import random
time_series = [2 * x + random() for x in range(1, 100)]
- Plot your data:
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(time_series)
plt.show()
The following screenshot shows the output:

- There is a large variety of techniques we can use to predict the consequent value of a time series:
- Autoregression (AR):
from statsmodels.tsa.ar_model import AR
model = AR(time_series)
model_fit = model.fit()
y = model_fit.predict(len(time_series), len(time_series))
-
- Moving average (MA):
from statsmodels.tsa.arima_model import ARMA
model = ARMA(time_series, order=(0, 1))
model_fit = model.fit(disp=False)
y = model_fit.predict(len(time_series), len(time_series))
-
- Simple exponential smoothing (SES):
from statsmodels.tsa.holtwinters import SimpleExpSmoothing
model = SimpleExpSmoothing(time_series)
model_fit = model.fit()
y = model_fit.predict(len(time_series), len(time_series))
The resulting predictions are as follows:
