-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathTimeSeries_AutoARIMA.py
177 lines (84 loc) · 2.29 KB
/
TimeSeries_AutoARIMA.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
# coding: utf-8
# In[1]:
# Importing necessary libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
import warnings
warnings.filterwarnings('ignore')
# In[2]:
#Installing pmdarima package
get_ipython().system(' pip install pmdarima')
# In[3]:
# Importing auto_arima
from pmdarima.arima import auto_arima
# In[4]:
#Read the sales dataset
sales_data = pd.read_csv("Champagne Sales.csv")
# In[5]:
sales_data.head()
# In[6]:
#Make sure there are no null values at the end of the dataset
sales_data.tail()
# In[7]:
#Check the datatypes
sales_data.dtypes
# In[8]:
#Convert the month column to datetime
sales_data['Month']=pd.to_datetime(sales_data['Month'])
# In[9]:
#Recheck the datatypes
sales_data.dtypes
# In[10]:
#Set the index of the Month
sales_data.set_index('Month',inplace=True)
# In[11]:
sales_data.head()
# In[12]:
# To understand the pattern
sales_data.plot()
# In[13]:
#Testing for stationarity
from pmdarima.arima import ADFTest
adf_test = ADFTest(alpha = 0.05)
adf_test.should_diff(sales_data)
# In[14]:
#Spliting the dataset into train and test
train = sales_data[:85]
test = sales_data[-20:]
# In[15]:
train.tail()
# In[16]:
test.head()
# In[17]:
plt.plot(train)
plt.plot(test)
# In[18]:
arima_model = auto_arima(train,start_p=0, d=1, start_q=0,
max_p=5, max_d=5, max_q=5, start_P=0,
D=1, start_Q=0, max_P=5, max_D=5,
max_Q=5, m=12, seasonal=True,
error_action='warn',trace = True,
supress_warnings=True,stepwise = True,
random_state=20,n_fits = 50 )
# In[19]:
#Summary of the model
arima_model.summary()
# In[20]:
prediction = pd.DataFrame(arima_model.predict(n_periods = 20),index=test.index)
prediction.columns = ['predicted_sales']
prediction
# In[21]:
plt.figure(figsize=(8,5))
plt.plot(train,label="Training")
plt.plot(test,label="Test")
plt.plot(prediction,label="Predicted")
plt.legend(loc = 'Left corner')
plt.show()
# In[22]:
from sklearn.metrics import r2_score
test['predicted_sales'] = prediction
r2_score(test['Champagne sales'], test['predicted_sales'])
# In[ ]: