-
Notifications
You must be signed in to change notification settings - Fork 1
/
Plastic_Sales_Py.py
192 lines (136 loc) · 8.1 KB
/
Plastic_Sales_Py.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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.tsa.holtwinters import SimpleExpSmoothing # SES
from statsmodels.tsa.holtwinters import Holt # Holts Exponential Smoothing
from statsmodels.tsa.holtwinters import ExponentialSmoothing #
# from datetime import datetime
plastic = pd.read_csv("C:/Data Science/Data Science/Assignments/Ass24. Forecasting/Plastic/PlasticSales_raw.csv")
plastic.Sales.plot() # time series plot
# Centering moving average for the time series
plastic.Sales.plot(label = "org")
for i in range(2, 9, 2):
plastic["Sales"].rolling(i).mean().plot(label = str(i))
plt.legend(loc = 3)
# Time series decomposition plot
decompose_ts_add = seasonal_decompose(plastic.Sales, model = "additive", period = 12)
decompose_ts_add.plot()
decompose_ts_mul = seasonal_decompose(plastic.Sales, model = "multiplicative", period = 12)
decompose_ts_mul.plot()
# splitting the data into Train and Test data
# Recent 12 time period values are Test data
Train = plastic.head(48)
Test = plastic.tail(12)
# Data Driven approach
# Creating a function to calculate the MAPE value for test data
def MAPE(pred,org):
temp = np.abs((pred-org)/org)*100
return np.mean(temp)
# Simple Exponential Method
ses_model = SimpleExpSmoothing(Train["Sales"]).fit()
pred_ses = ses_model.predict(start = Test.index[0], end = Test.index[-1])
MAPE(pred_ses, Test.Sales)
# Holt method
hw_model = Holt(Train["Sales"]).fit()
pred_hw = hw_model.predict(start = Test.index[0], end = Test.index[-1])
MAPE(pred_hw, Test.Sales)
# Holts winter exponential smoothing with additive seasonality and additive trend
hwe_model_add_add = ExponentialSmoothing(Train["Sales"], seasonal = "add", trend = "add", seasonal_periods = 12).fit()
pred_hwe_add_add = hwe_model_add_add.predict(start = Test.index[0], end = Test.index[-1])
MAPE(pred_hwe_add_add, Test.Sales)
# Holts winter exponential smoothing with multiplicative seasonality and additive trend
hwe_model_mul_add = ExponentialSmoothing(Train["Sales"], seasonal = "mul", trend = "add", seasonal_periods = 12).fit()
pred_hwe_mul_add = hwe_model_mul_add.predict(start = Test.index[0], end = Test.index[-1])
MAPE(pred_hwe_mul_add, Test.Sales)
# Final Model on 100% Data
hwe_model_add_add = ExponentialSmoothing(plastic["Sales"], seasonal = "add", trend = "add", seasonal_periods = 12).fit()
# Load the new data which includes the entry for future 12 values
new_data = pd.read_csv("C:/Data Science/Data Science/Assignments/Ass24. Forecasting/Plastic/New_PlasticSales.csv")
newdata_pred = hwe_model_add_add.predict(start = new_data.index[0], end = new_data.index[-1])
newdata_pred
#########################################################
##################################################
#####################################
#Model Based approach
import pandas as pd
Plastic = pd.read_csv("C:/Data Science/Data Science/Assignments/Ass24. Forecasting/Plastic/PlasticSales_raw.csv")
month = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
# Pre processing
import numpy as np
Plastic["t"] = np.arange(0, 60)
Plastic["t_square"] = Plastic["t"]*Plastic["t"]
Plastic["log_Sales"] = np.log(Plastic["Sales"])
Plastic.columns
p = Plastic["Month"][0]
p[0:3]
Plastic['months']= 0
for i in range(59):
p = Plastic["Month"][i]
Plastic['months'][i]= p[0:3]
month_dummies = pd.DataFrame(pd.get_dummies(Plastic['months']))
Plastic1 = pd.concat([Plastic, month_dummies], axis = 1)
# Visualization - Time plot
Plastic1.Sales.plot()
# Data Partition
Train = Plastic1.head(48)
Test = Plastic1.tail(12)
# to change the index value in pandas data frame
# Test.set_index(np.arange(1,13))
####################### L I N E A R ##########################
import statsmodels.formula.api as smf
linear_model = smf.ols('Sales ~ t', data=Train).fit()
pred_linear = pd.Series(linear_model.predict(pd.DataFrame(Test['t'])))
rmse_linear = np.sqrt(np.mean((np.array(Test['Sales']) - np.array(pred_linear))**2))
rmse_linear
##################### Exponential ##############################
Exp = smf.ols('log_Sales ~ t', data = Train).fit()
pred_Exp = pd.Series(Exp.predict(pd.DataFrame(Test['t'])))
rmse_Exp = np.sqrt(np.mean((np.array(Test['Sales']) - np.array(np.exp(pred_Exp)))**2))
rmse_Exp
#################### Quadratic ###############################
Quad = smf.ols('Sales ~ t+t_square', data=Train).fit()
pred_Quad = pd.Series(Quad.predict(Test[["t","t_square"]]))
rmse_Quad = np.sqrt(np.mean((np.array(Test['Sales'])-np.array(pred_Quad))**2))
rmse_Quad
################### Additive seasonality ########################
add_sea = smf.ols('Sales ~ Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sep+Oct+Nov', data=Train).fit()
pred_add_sea = pd.Series(add_sea.predict(Test[['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov']]))
rmse_add_sea = np.sqrt(np.mean((np.array(Test['Sales'])-np.array(pred_add_sea))**2))
rmse_add_sea
################## Multiplicative Seasonality ##################
Mul_sea = smf.ols('log_Sales ~ Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sep+Oct+Nov',data = Train).fit()
pred_Mult_sea = pd.Series(Mul_sea.predict(Test[['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov']]))
rmse_Mult_sea = np.sqrt(np.mean((np.array(Test['Sales']) - np.array(np.exp(pred_Mult_sea)))**2))
rmse_Mult_sea
################### Additive seasonality with linear trend ########################
lin_add_sea = smf.ols('Sales ~ t+Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sep+Oct+Nov', data=Train).fit()
pred_lin_add_sea = pd.Series(lin_add_sea.predict(Test[['t','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov']]))
rmse_lin_add_sea = np.sqrt(np.mean((np.array(Test['Sales'])-np.array(pred_lin_add_sea))**2))
rmse_lin_add_sea
################## Additive Seasonality Quadratic Trend ############################
add_sea_Quad = smf.ols('Sales ~ t+t_square+Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sep+Oct+Nov',data=Train).fit()
pred_add_sea_quad = pd.Series(add_sea_Quad.predict(Test[['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','t','t_square']]))
rmse_add_sea_quad = np.sqrt(np.mean((np.array(Test['Sales'])-np.array(pred_add_sea_quad))**2))
rmse_add_sea_quad
################## Multiplicative Seasonality Linear Trend ###########
Mul_Add_sea = smf.ols('log_Sales ~ t+Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sep+Oct+Nov',data = Train).fit()
pred_Mult_add_sea = pd.Series(Mul_Add_sea.predict(Test[['t','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov']]))
rmse_Mult_add_sea = np.sqrt(np.mean((np.array(Test['Sales'])-np.array(np.exp(pred_Mult_add_sea)))**2))
rmse_Mult_add_sea
################## Multiplicative Seasonality Linear Trend ###########
Mul_Add_sea_Quad = smf.ols('log_Sales ~ t+t_square+Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sep+Oct+Nov',data = Train).fit()
pred_Mult_add_sea_Quad = pd.Series(Mul_Add_sea_Quad.predict(Test[['t','t_square','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov']]))
rmse_Mult_add_sea_Quad = np.sqrt(np.mean((np.array(Test['Sales'])-np.array(np.exp(pred_Mult_add_sea_Quad)))**2))
rmse_Mult_add_sea_Quad
################## Testing #######################################
data = {"MODEL":pd.Series(["rmse_linear","rmse_Exp","rmse_Quad","rmse_add_sea","rmse_lin_add_sea","rmse_add_sea_quad","rmse_Mult_sea","rmse_Mult_add_sea","rmse_Mult_add_sea_Quad"]),"RMSE_Values":pd.Series([rmse_linear,rmse_Exp,rmse_Quad,rmse_add_sea,rmse_lin_add_sea,rmse_add_sea_quad,rmse_Mult_sea,rmse_Mult_add_sea,rmse_Mult_add_sea_Quad])}
table_rmse=pd.DataFrame(data)
table_rmse
# 'rmse_Mult_add_sea' has the least value among the models prepared so far Predicting new values
predict_data1 = pd.read_csv("C:/Data Science/Data Science/Assignments/Ass24. Forecasting/Plastic/PlasticSales_pred.csv")
model_full = smf.ols('Sales ~ t+Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sep+Oct+Nov', data = Plastic1).fit()
pred_new = pd.Series(model_full.predict(predict_data1))
pred_new
predict_data1["forecasted_Sales"] = pd.Series(pred_new)