Da 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

IOT DA5

SUYASH AGARWAL 20BBS0123


Using ARIMA model to predict air passenger for an airline

dataset: https://www.kaggle.com/datasets/chirag19/air-passengers/data

from datetime import datetime


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

from statsmodels.tsa.stattools import adfuller


from statsmodels.tsa.stattools import acf, pacf
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.tsa.arima.model import ARIMA

data = pd.read_csv('/content/AirPassengers.csv')
data['Date'] = pd.to_datetime(data['Month'])
data = data.drop(columns = 'Month')
data = data.set_index('Date')
data = data.rename(columns = {'#Passengers':'Passengers'})
data.head()

output Passengers

Date

1949-01-01 112

1949-02-01 118

1949-03-01 132

1949-04-01 129

1949-05-01 121

Model

model = ARIMA(data['Passengers'],order = (2,1,2))


model_fit = model.fit()
print(model_fit.summary())

/usr/local/lib/python3.10/dist-packages/statsmodels/tsa/base/tsa_model.py:473: ValueWarning: No frequency information was provided,


self._init_dates(dates, freq)
/usr/local/lib/python3.10/dist-packages/statsmodels/tsa/base/tsa_model.py:473: ValueWarning: No frequency information was provided,
self._init_dates(dates, freq)
/usr/local/lib/python3.10/dist-packages/statsmodels/tsa/base/tsa_model.py:473: ValueWarning: No frequency information was provided,
self._init_dates(dates, freq)
SARIMAX Results
==============================================================================
Dep. Variable: Passengers No. Observations: 144
Model: ARIMA(2, 1, 2) Log Likelihood -671.673
Date: Tue, 31 Oct 2023 AIC 1353.347
Time: 16:35:28 BIC 1368.161
Sample: 01-01-1949 HQIC 1359.366
- 12-01-1960
Covariance Type: opg
==============================================================================
coef std err z P>|z| [0.025 0.975]
------------------------------------------------------------------------------
ar.L1 1.6850 0.020 83.061 0.000 1.645 1.725
ar.L2 -0.9549 0.017 -55.420 0.000 -0.989 -0.921
ma.L1 -1.8432 0.124 -14.845 0.000 -2.087 -1.600
ma.L2 0.9953 0.135 7.398 0.000 0.732 1.259
sigma2 665.9646 113.887 5.848 0.000 442.751 889.178
===================================================================================
Ljung-Box (L1) (Q): 0.30 Jarque-Bera (JB): 1.84
Prob(Q): 0.59 Prob(JB): 0.40
Heteroskedasticity (H): 7.38 Skew: 0.27
Prob(H) (two-sided): 0.00 Kurtosis: 3.14
===================================================================================

Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).
/usr/local/lib/python3.10/dist-packages/statsmodels/base/model.py:607: ConvergenceWarning: Maximum Likelihood optimization failed t
warnings.warn("Maximum Likelihood optimization failed to "
Predictions

size = int(len(data) - 30)


train, test = data['Passengers'][0:size], data['Passengers'][size:len(data)]

print('\t ARIMA MODEL : In- Sample Forecasting \n')

history = [x for x in train]


predictions = []

for t in range(len(test)):

model = ARIMA(history, order=(2,1,2))


model_fit = model.fit()

output = model_fit.forecast()
yhat = output[0]
predictions.append(float(yhat))

obs = test[t]
history.append(obs)

print('predicted = %f, expected = %f' % (yhat, obs))

ARIMA MODEL : In- Sample Forecasting

predicted = 435.368729, expected = 491.000000


predicted = 487.825559, expected = 505.000000
predicted = 484.761914, expected = 404.000000
predicted = 359.074739, expected = 359.000000
predicted = 380.902398, expected = 310.000000
predicted = 291.230227, expected = 337.000000
predicted = 336.937875, expected = 360.000000
predicted = 367.624722, expected = 342.000000
predicted = 360.295134, expected = 406.000000
predicted = 414.083702, expected = 396.000000
predicted = 378.520878, expected = 420.000000
/usr/local/lib/python3.10/dist-packages/statsmodels/base/model.py:607: ConvergenceWarning: Maximum Likelihood optimization failed t
warnings.warn("Maximum Likelihood optimization failed to "
predicted = 424.474755, expected = 472.000000
predicted = 459.477027, expected = 548.000000
predicted = 534.244080, expected = 559.000000
/usr/local/lib/python3.10/dist-packages/statsmodels/base/model.py:607: ConvergenceWarning: Maximum Likelihood optimization failed t
warnings.warn("Maximum Likelihood optimization failed to "
predicted = 523.516869, expected = 463.000000
predicted = 417.605516, expected = 407.000000
/usr/local/lib/python3.10/dist-packages/statsmodels/base/model.py:607: ConvergenceWarning: Maximum Likelihood optimization failed t
warnings.warn("Maximum Likelihood optimization failed to "
predicted = 366.683909, expected = 362.000000
/usr/local/lib/python3.10/dist-packages/statsmodels/base/model.py:607: ConvergenceWarning: Maximum Likelihood optimization failed t
warnings.warn("Maximum Likelihood optimization failed to "
predicted = 339.217575, expected = 405.000000
predicted = 411.312454, expected = 417.000000
predicted = 435.937844, expected = 391.000000
predicted = 418.796173, expected = 419.000000
predicted = 451.314963, expected = 461.000000
predicted = 488.383979, expected = 472.000000
predicted = 488.569136, expected = 535.000000
predicted = 532.219019, expected = 622.000000
/usr/local/lib/python3.10/dist-packages/statsmodels/base/model.py:607: ConvergenceWarning: Maximum Likelihood optimization failed t
warnings.warn("Maximum Likelihood optimization failed to "
predicted = 601.265999, expected = 606.000000
/usr/local/lib/python3.10/dist-packages/statsmodels/base/model.py:607: ConvergenceWarning: Maximum Likelihood optimization failed t
warnings.warn("Maximum Likelihood optimization failed to "
predicted = 563.028797, expected = 508.000000
/usr/local/lib/python3.10/dist-packages/statsmodels/base/model.py:607: ConvergenceWarning: Maximum Likelihood optimization failed t
warnings.warn("Maximum Likelihood optimization failed to "
predicted = 452.854957, expected = 461.000000
/usr/local/lib/python3.10/dist-packages/statsmodels/base/model.py:607: ConvergenceWarning: Maximum Likelihood optimization failed t
warnings.warn("Maximum Likelihood optimization failed to "
predicted = 415.004912, expected = 390.000000
predicted = 367.692302, expected = 432.000000
/usr/local/lib/python3.10/dist-packages/statsmodels/base/model.py:607: ConvergenceWarning: Maximum Likelihood optimization failed t
warnings.warn("Maximum Likelihood optimization failed to "

predictions_series = pd.Series(predictions, index = test.index)


fig,ax = plt.subplots(nrows = 1,ncols = 1,figsize = (15,5))

plt.subplot(1,1,1)
plt.plot(data['Passengers'],label = 'Expected Values')
plt.plot(predictions_series,label = 'Predicted Values');
plt.legend(loc="upper left")
plt.show()

RMSE SCORE

from sklearn.metrics import mean_squared_error


error = np.sqrt(mean_squared_error(test,predictions))
print('Test RMSE: %.4f' % error)

Test RMSE: 45.3502

You might also like