-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
209 lines (79 loc) · 3.16 KB
/
model.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings('ignore')
import matplotlib.pyplot as plt
import seaborn as sns
from time import gmtime, strftime
from pylab import rcParams
import statsmodels.api as sm
import itertools
from statsmodels.tsa.statespace.sarimax import SARIMAX
from itertools import product
from pandas.tseries.offsets import DateOffset
invoice = pd.read_csv("Final_invoice.csv",low_memory=False)
invoice['Month-Year'] = pd.to_datetime(invoice['Invoice Date']).dt.strftime('20%y-%m')
# In[4]:
revenue_model = invoice.groupby(['Model','Month-Year']).agg({'Total Amt Wtd Tax.':['mean','count']})
# In[5]:
# In[6]:
revenue_model.columns = revenue_model.columns.droplevel()
# In[7]:
revenue_model['Revenue'] = revenue_model['mean'] * revenue_model['count']
# In[8]:
# In[9]:
model_data = revenue_model.round(1).pivot_table(index = ['Month-Year'], columns = ['Model'], values = ['Revenue'], fill_value=0, aggfunc='sum').reset_index()
# In[10]:
# In[11]:
model_data.columns = model_data.columns.droplevel()
# In[12]:
model_data.rename(columns={'':'Month-Year'},inplace=True)
# In[13]:
# In[14]:
model_data['Total_Revenue'] = model_data.iloc[:,1:].sum(axis=1)
# In[15]:
# In[16]:
#model_data.to_csv('model data.csv')
# In[17]:
Time_Series_data = model_data[['Total_Revenue','Month-Year']].sort_values(by = 'Month-Year')
Time_Series_data['Month-Year'] = pd.to_datetime(Time_Series_data['Month-Year'])
Time_Series_data.set_index('Month-Year',inplace=True)
# In[18]:
# In[19]:
result = sm.tsa.seasonal_decompose(Time_Series_data['Total_Revenue'], model='multiplicative')
# In[20]:
# In[21]:
x = Time_Series_data['Total_Revenue'].values
# In[27]:
#p=d=q=range(0,9)
# In[28]:
#pdq=list(product(p,d,q))
# In[29]:
#seasonal_pdq = [(x[0], x[1], x[2], 12) for x in pdq]
# In[30]:
sarima = SARIMAX(x,order=(0,0,0),seasonal_order = (0, 7, 0, 12))
sarima_fit = sarima.fit()
# In[ ]:
# In[ ]:
data = int(input(" Enter the Number of Months: "))
dates = [Time_Series_data["Total_Revenue"].index[-1] + DateOffset(months=x) for x in range(0,data) ]
f_date = pd.DataFrame(index=dates[1:],columns = Time_Series_data.columns)
dataset = pd.concat([Time_Series_data,f_date])
prediction_sArima = sarima_fit.predict(start = 55, end = dataset.shape[0])
dataset = dataset.drop(index = dataset.index,inplace = True)
print(prediction_sArima.astype(np.int64))
# In[ ]:
#data = int(input(" Enter the Number of Month: "))
#Year = int(input(" Enter the Number of Year"))
#dates = [Time_Series_data["Total_Revenue"].index[-1] + DateOffset(month=x) for x in range(0,data) ]
#Years = [Time_Series_data["Total_Revenue"].index[-1] + DateOffset(Year=x) for x in range(0,Year)]
#f_date = pd.DataFrame(index=dates[1:],columns = Time_Series_data.columns)
#dataset = pd.concat([Time_Series_data,f_date])
#prediction_sArima = sarima_fit.predict(start = 55, end = dataset.shape[0])
#dataset = dataset.drop(index = dataset.index,inplace = True)
#print(prediction_sArima.astype(np.int64))
import pickle
# Open a file
file = open('model.pkl', 'wb')
# dump information to that file
pickle.dump(prediction_sArima,file)