You are on page 1of 1

In 

[ ]: import pandas as pd

import numpy as np

import matplotlib.pylab as plt

%matplotlib inline

In [ ]: from google.colab import files

uploaded = files.upload()

Choose Files No file chosen


Upload widget is
only available when the cell has been executed in the
current
browser session. Please rerun this cell to enable.

Saving AirPassengers.csv to AirPassengers.csv

In [ ]: data = pd.read_csv('AirPassengers.csv')

data.head()

Out[ ]: Month #Passengers

0 1949-01 112

1 1949-02 118

2 1949-03 132

3 1949-04 129

4 1949-05 121

In [ ]: from datetime import datetime

con=data['Month']

data['Month']=pd.to_datetime(data['Month'])

data.set_index('Month', inplace=True)

#check datatype of index

data.index

DatetimeIndex(['1949-01-01', '1949-02-01', '1949-0


Out[ ]:
3-01', '1949-04-01',

'1949-05-01', '1949-06-01', '1949-0


7-01', '1949-08-01',

'1949-09-01', '1949-10-01',

...

'1960-03-01', '1960-04-01', '1960-0


5-01', '1960-06-01',

'1960-07-01', '1960-08-01', '1960-0


9-01', '1960-10-01',

'1960-11-01', '1960-12-01'],

dtype='datetime64[ns]', name='Mont
h', length=144, freq=None)

In [ ]: ts = data['#Passengers']

ts.head(10)

Month
Out[ ]:
1949-01-01 112

1949-02-01 118

1949-03-01 132

1949-04-01 129

1949-05-01 121

1949-06-01 135

1949-07-01 148

1949-08-01 148

1949-09-01 136

1949-10-01 119

Name: #Passengers, dtype: int64

In [ ]: plt.plot(ts)

[<matplotlib.lines.Line2D at 0x7ff7a315d110>]
Out[ ]:

In [ ]: rolmean = ts.rolling(13).mean()

rolstd = ts.rolling(13).std()

plt.plot(ts, color='blue',label='Original')

plt.plot(rolmean, color='red', label='Rolling Mean


plt.plot(rolstd, color='black', label = 'Rolling St
plt.legend(loc='best')

plt.title('Rolling Mean & Standard Deviation')


plt.show()

In [ ]: ts_log=np.log(ts)

plt.plot(ts_log)

[<matplotlib.lines.Line2D at 0x7ff7a2bb2490>]
Out[ ]:

In [ ]: # moving_avg= ts_log.rolling(13).mean()

# print(moving_avg.dtypes)

# plt.plot(ts_log,label='Original')

# plt.plot(moving_avg, color='red',label='Rolling M
# plt.legend()

float64

<matplotlib.legend.Legend at 0x7ff7a2b2c4d0>
Out[ ]:

In [ ]: # ts_log_moving_avg_diff=ts_log-moving_avg

# ts_log_moving_avg_diff.head(13)

Month
Out[ ]:
1949-01-01 NaN

1949-02-01 NaN

1949-03-01 NaN

1949-04-01 NaN

1949-05-01 NaN

1949-06-01 NaN

1949-07-01 NaN

1949-08-01 NaN

1949-09-01 NaN

1949-10-01 NaN

1949-11-01 NaN

1949-12-01 NaN

1950-01-01 NaN

Name: #Passengers, dtype: float64

In [ ]: # ts_log_moving_avg_diff.dropna(inplace=True)

# ts_log_moving_avg_diff.head(10)

# moving_avg= ts_log_moving_avg_diff.rolling(13).me

In [ ]: # plt.plot(ts_log_moving_avg_diff,label='Original')
# plt.plot(moving_avg,color='red',label='Rolling Me
# plt.legend()

In [ ]: ts_log_diff=ts_log-ts_log.shift()

plt.plot(ts_log_diff)

[<matplotlib.lines.Line2D at 0x7ff78e8a61d0>]
Out[ ]:

In [ ]: ts_log_diff.dropna(inplace=True)

moving_avg= ts_log_diff.rolling(13).mean()

plt.plot(ts_log_diff)

plt.plot(moving_avg,color='red',label='Rolling Mean
plt.legend()

<matplotlib.legend.Legend at 0x7ff78e95db90>
Out[ ]:

In [ ]: from statsmodels.tsa.arima_model import ARIMA

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

results_ARIMA=model.fit(disp=-1)

plt.plot(ts_log_diff,label='Original')

plt.plot(results_ARIMA.fittedvalues,color='red',lab
plt.legend()

/usr/local/lib/python3.7/dist-packages/statsmodel
s/tsa/base/tsa_model.py:165: ValueWarning: No freq
uency information was provided, so inferred freque
ncy MS will be used.

% freq, ValueWarning)

/usr/local/lib/python3.7/dist-packages/statsmodel
s/tsa/base/tsa_model.py:165: ValueWarning: No freq
uency information was provided, so inferred freque
ncy MS will be used.

% freq, ValueWarning)

/usr/local/lib/python3.7/dist-packages/statsmodel
s/base/model.py:492: HessianInversionWarning: Inve
rting hessian failed, no bse or cov_params availab
le

'available', HessianInversionWarning)

<matplotlib.legend.Legend at 0x7ff78e8eaf10>
Out[ ]:

In [ ]:

In [ ]:

You might also like