简体   繁体   中英

Get better fit on test data using Auto_Arima

I am using the AirPassengers dataset to predict a timeseries. For the model I am using, I chosen to use auto_arima to forecast the predicted values. However, it seems that the chosen order by the auto_arima is unable to fit the model. The corresponding chart is produced.

预测的

What can I do to get a better fit?

My code for those that want to try:

import pandas as pd
import numpy as np
import matplotlib.pylab as plt
%matplotlib inline

from pmdarima import auto_arima

df = pd.read_csv("https://raw.githubusercontent.com/AileenNielsen/TimeSeriesAnalysisWithPython/master/data/AirPassengers.csv")
df = df.rename(columns={"#Passengers":"Passengers"})
df.Month = pd.to_datetime(df.Month)
df.set_index('Month',inplace=True)

train,test=df[:-24],df[-24:]

model = auto_arima(train,trace=True,error_action='ignore', suppress_warnings=True)
model.fit(train)

forecast = model.predict(n_periods=24)
forecast = pd.DataFrame(forecast,index = test.index,columns=['Prediction'])

plt.plot(train, label='Train')
plt.plot(test, label='Valid')
plt.plot(forecast, label='Prediction')
plt.show()

from sklearn.metrics import mean_squared_error
print(mean_squared_error(test['Passengers'],forecast['Prediction']))

Thank you for reading. Any advice is appreciated.

The problem was that I did not specify the m, in this case, I assigned the value of m to be 12, denoting that it is a monthly cycle, that each data row is a month. That's how I understand it.source

Feel free to comment, I'm not entirely sure as I am new to using ARIMA.

Code:

model = auto_arima(train,m=12,trace=True,error_action='ignore', suppress_warnings=True)

Just add m=12,to denote that the data is monthly.

Result: 我想要的是

This series is not stationary, and no amount of differencing (notice that the amplitude of the variations keeps increasing) will make it so. However, transforming the data first by taking logs should do better (experiment shows that it does do better, but not what I would call well). Setting the seasonality (as I suggest in the comment by m=12, and taking logs produces this: 在此处输入图像描述 which is essentially perfect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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