-
Notifications
You must be signed in to change notification settings - Fork 0
/
XGBoost&RandomForest
245 lines (176 loc) · 8.13 KB
/
XGBoost&RandomForest
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
####Train random forest model
from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import numpy as np
import pandas as pd
from math import sqrt
import time
# Load the dataset
df1 = pd.read_csv('../input/sales-forecasting-womart-store/TRAIN.csv')
df= df1.drop_duplicates()
# Convert the 'Date' column to datetime format
df['Date'] = pd.to_datetime(df['Date'])
# Extract time-related features
df['Week'] = df['Date'].dt.isocalendar().week
df['Day'] = df['Date'].dt.day
df['Month'] = df['Date'].dt.month
df['Year'] = df['Date'].dt.year
# Drop the "ID" and "Date" columns
df = df.drop(['ID', 'Date'], axis=1)
# Split the data into training and testing sets
train_data, test_data = train_test_split(df, test_size=0.2, random_state=42)
# Define categorical columns to be one-hot encoded along with new time-related features
categorical_cols = ['Store_Type', 'Location_Type', 'Region_Code', 'Discount', 'Week', 'Day', 'Month', 'Year', 'Holiday']
# Create a ColumnTransformer for one-hot encoding
preprocessor = ColumnTransformer(
transformers=[('cat', OneHotEncoder(drop='first', sparse=False), categorical_cols)],
remainder='passthrough'
)
# Fit the ColumnTransformer on the training data and transform both training and test data
X_train = preprocessor.fit_transform(train_data.drop('Sales', axis=1))
X_test = preprocessor.transform(test_data.drop('Sales', axis=1))
y_train = train_data['Sales']
y_test = test_data['Sales']
# Train the Random Forest model
rf_model = RandomForestRegressor(n_estimators=50, max_depth=5)
rf_model.fit(X_train, y_train)
predictions_rf = rf_model.predict(X_test)
# Evaluate the model
rf_rmse = sqrt(mean_squared_error(y_test, predictions_rf))
print(f"Random Forest RMSE: {rf_rmse}")
###tune hyperparameters with grid search for random forest
from sklearn.model_selection import GridSearchCV
# Step 1: Tune 'n_estimators' keeping 'max_depth' at a reasonable fixed value (e.g., 5)
# Create the parameter grid for 'n_estimators'
param_grid_1 = {
'n_estimators': [50, 100, 200]
}
# Create a based model
rf = RandomForestRegressor(max_depth=5)
# Instantiate the grid search model
grid_search_1 = GridSearchCV(estimator = rf, param_grid = param_grid_1,
cv = 3, n_jobs = -1, verbose = 2)
# Fit the grid search to the data
grid_search_1.fit(X_train, y_train)
# Get the best 'n_estimators' parameter
best_n_estimators = grid_search_1.best_params_['n_estimators']
best_n_estimators
# Step 2: Tune 'max_depth' using the best 'n_estimators' value found (which is 100)
# Create the parameter grid for 'max_depth'
param_grid_2 = {
'max_depth': [5, 10, 15]
}
# Create a based model with the best 'n_estimators'
rf = RandomForestRegressor(n_estimators=100) # Using 100 as the best_n_estimators value
# Instantiate the grid search model
grid_search_2 = GridSearchCV(estimator = rf, param_grid = param_grid_2,
cv = 3, n_jobs = -1, verbose = 2)
# Fit the grid search to the data
grid_search_2.fit(X_train, y_train)
# Get the best 'max_depth' parameter
best_max_depth = grid_search_2.best_params_['max_depth']
# Train the Random Forest model with best parameters
best_rf_model = RandomForestRegressor(n_estimators=100, max_depth=best_max_depth)
best_rf_model.fit(X_train, y_train)
predictions_best_rf = best_rf_model.predict(X_test)
# Evaluate the tuned model
best_rf_rmse = sqrt(mean_squared_error(y_test, predictions_best_rf))
print(f"Tuned Random Forest RMSE: {best_rf_rmse}")
### Train XGBoost model
# Train the XGBoost model
xgb_model = XGBRegressor(objective='reg:squarederror', n_estimators=50, max_depth=5)
xgb_model.fit(X_train, y_train)
predictions_xgb = xgb_model.predict(X_test)
# Evaluate the model
xgboost_rmse = sqrt(mean_squared_error(y_test, predictions_xgb))
print(f"XGboost RMSE: {xgboost_rmse}")
#Tunning XGBoost
# Step 1: Tune 'n_estimators' keeping 'max_depth' and 'learning_rate' at default values
# Create the parameter grid for 'n_estimators'
param_grid_xgb_1 = {
'n_estimators': [50, 100, 200]
}
# Create a based model
xgb = XGBRegressor(objective='reg:squarederror')
# Instantiate the grid search model
grid_search_xgb_1 = GridSearchCV(estimator=xgb, param_grid=param_grid_xgb_1,
cv=3, n_jobs=-1, verbose=2)
# Fit the grid search to the data
grid_search_xgb_1.fit(X_train, y_train)
# Get the best 'n_estimators' parameter
best_n_estimators_xgb = grid_search_xgb_1.best_params_['n_estimators']
best_n_estimators_xgb
# Step 2: Tune 'max_depth' using the best 'n_estimators' value found (which is 200)
# Create the parameter grid for 'max_depth'
param_grid_xgb_2 = {
'max_depth': [3, 5, 7]
}
# Create a based model with the best 'n_estimators'
xgb = XGBRegressor(objective='reg:squarederror', n_estimators=200) # Using 200 as the best_n_estimators_xgb value
# Instantiate the grid search model
grid_search_xgb_2 = GridSearchCV(estimator=xgb, param_grid=param_grid_xgb_2,
cv=3, n_jobs=-1, verbose=2)
# Fit the grid search to the data
grid_search_xgb_2.fit(X_train, y_train)
# Get the best 'max_depth' parameter
best_max_depth_xgb = grid_search_xgb_2.best_params_['max_depth']
# Train the XGBoost model with best parameters
best_xgb_model = XGBRegressor(objective='reg:squarederror', n_estimators=200, max_depth=best_max_depth_xgb)
best_xgb_model.fit(X_train, y_train)
predictions_best_xgb = best_xgb_model.predict(X_test)
# Evaluate the tuned model
best_xgb_rmse = sqrt(mean_squared_error(y_test, predictions_best_xgb))
best_xgb_rmse
print(f"Tuned XGBoost RMSE: {best_rf_rmse}")
#Tune regularization terms (reg_alpha and reg_lambda using the best n_estimators and max_depth)
# Step 3: Tune 'reg_alpha' and 'reg_lambda' using the best 'n_estimators' and 'max_depth' values found
# Create the parameter grid for 'reg_alpha' and 'reg_lambda'
param_grid_xgb_3 = {
'reg_alpha': [0, 0.001, 0.005, 0.01, 0.05],
'reg_lambda': [0.5, 1, 1.5, 2]
}
# Create a based model with the best 'n_estimators' and 'max_depth'
xgb = XGBRegressor(objective='reg:squarederror', n_estimators=best_n_estimators_xgb, max_depth=best_max_depth_xgb)
# Instantiate the grid search model
grid_search_xgb_3 = GridSearchCV(estimator=xgb, param_grid=param_grid_xgb_3,
cv=3, n_jobs=-1, verbose=2)
# Fit the grid search to the data
grid_search_xgb_3.fit(X_train, y_train)
# Get the best 'reg_alpha' and 'reg_lambda' parameters
best_reg_alpha_xgb = grid_search_xgb_3.best_params_['reg_alpha']
best_reg_lambda_xgb = grid_search_xgb_3.best_params_['reg_lambda']
# Train the XGBoost model with best parameters
best_xgb_model = XGBRegressor(objective='reg:squarederror',
n_estimators=best_n_estimators_xgb,
max_depth=best_max_depth_xgb,
reg_alpha=best_reg_alpha_xgb,
reg_lambda=best_reg_lambda_xgb)
best_xgb_model.fit(X_train, y_train)
predictions_best_xgb = best_xgb_model.predict(X_test)
# Evaluate the tuned model
best_xgb_rmse = sqrt(mean_squared_error(y_test, predictions_best_xgb))
print(f"Tuned XGBoost RMSE: {best_xgb_rmse}")
###Ensemble models before tunning
# Ensemble the predictions
final_predictions = (predictions_rf + predictions_xgb) / 2
# Evaluate the ensemble model
ensemble_rmse = sqrt(mean_squared_error(y_test, final_predictions))
print(f"Ensemble RMSE: {ensemble_rmse}")
###Ensemble models after tunning with weighted average
# Calculate the inverse of each model's RMSE as the weight
weight_xgb = 1 / best_xgb_rmse
weight_rf = 1 / best_rf_rmse
# Calculate the total weight
total_weight = weight_xgb + weight_rf
# Normalize the weights
normalized_weight_xgb = weight_xgb / total_weight
normalized_weight_rf = weight_rf / total_weight
# Ensemble the predictions
ensemble_tuned_predictions = (predictions_best_rf*normalized_weight_rf) + (predictions_best_xgb*normalized_weight_xgb)
# Evaluate the ensemble model
ensemble_tuned_rmse = sqrt(mean_squared_error(y_test, ensemble_tuned_predictions))
ensemble_tuned_rmse