-
Notifications
You must be signed in to change notification settings - Fork 2
/
Model_Code.R
449 lines (344 loc) · 15.4 KB
/
Model_Code.R
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
install.packages("caTools")
install.packages("randomForest")
#Importing dataset
library(xgboost)
library(Matrix)
library(caTools)
library(randomForest)
library(plyr)
dataset = read.csv("C:/Users/sriram rajagopalan/Downloads/Uconn/R Proj/PCA_data_target_500.csv")
#Training - test split
set.seed(123)
split = sample.split(dataset$Level_Zero, SplitRatio = 0.75)
training_set = subset(dataset, split==TRUE)
test_set = subset(dataset, split == FALSE)
training_set1 = training_set[-1:-10]
test_set1 = test_set[-1:-10]
#---------------------------------------------------------------------------------------------
#RandomForest
#Level 0
#Target Variable
Y_Target = training_set$Level_Zero
#Fitting the model in Training Dataset
rf_model = randomForest(x = training_set1, y= Y_Target,ntree=500)
#Predict test results
pred_rf_0 = predict(rf_model, newdata=test_set1)
predictions_0 = (ifelse(pred_rf_0>0.5,1,0))
#Confusion Matrix
actual_0 = test_set$Level_Zero
table(actual_0,predictions_0)
#--------------------------------------------------------------------------------------------
#Level 1
#Target Variable
Y_Target = training_set$Level_One
#Fitting the model in Training Dataset
rf_model_1 = randomForest(x = training_set1, y= Y_Target,ntree=500)
#Predict test results
pred_rf_1 = predict(rf_model_1, newdata=test_set1)
predictions_1 = (ifelse(pred_rf_1>0.5,1,0))
#Confusion Matrix
actual_1 = test_set$Level_One
table(actual_1,predictions_1)
#--------------------------------------------------------------------------------------------
#Level 2
#Target Variable
Y_Target = training_set$Level_two
#Fitting the model in Training Dataset
rf_model_2 = randomForest(x = training_set1, y= Y_Target,ntree=500)
#Predict test results
pred_rf_2 = predict(rf_model_2, newdata=test_set1)
predictions_2 = (ifelse(pred_rf_2>0.5,1,0))
#Confusion Matrix
actual_2 = test_set$Level_two
table(actual_2,predictions_2)
#--------------------------------------------------------------------------------------------
#Level 3
#Target Variable
Y_Target = training_set$Level_three
#Fitting the model in Training Dataset
rf_model_3 = randomForest(x = training_set1, y= Y_Target,ntree=500)
#Predict test results
pred_rf_3 = predict(rf_model_3, newdata=test_set1)
predictions_3 = (ifelse(pred_rf_3>0.5,1,0))
#Confusion Matrix
actual_3 = test_set$Level_three
table(actual_3,predictions_3)
#-------------------------------------------------------------------------------------------
#Level 4
#Target Variable
Y_Target = training_set$Level_four
#Fitting the model in Training Dataset
rf_model_4 = randomForest(x = training_set1, y= Y_Target,ntree=500)
#Predict test results
pred_rf_4 = predict(rf_model_4, newdata=test_set1)
predictions_4 = (ifelse(pred_rf_4>0.5,1,0))
#Confusion Matrix
actual_4 = test_set$Level_four
table(actual_4,predictions_4)
#-------------------------------------------------------------------------------------------
#Level 5
#Target Variable
Y_Target = training_set$Level_five
#Fitting the model in Training Dataset
rf_model_5 = randomForest(x = training_set1, y= Y_Target,ntree=500)
#Predict test results
pred_rf_5 = predict(rf_model_5, newdata=test_set1)
predictions_5 = (ifelse(pred_rf_5>0.5,1,0))
#Confusion Matrix
actual_5 = test_set$Level_five
table(actual_5,predictions_5)
#--------------------------------------------------------------------------------------------
#Level 6
#Target Variable
Y_Target = training_set$Level_six
#Fitting the model in Training Dataset
rf_model_6 = randomForest(x = training_set1, y= Y_Target,ntree=500)
#Predict test results
pred_rf_6 = predict(rf_model_6, newdata=test_set1)
predictions_6 = (ifelse(pred_rf_6>0.5,1,0))
#Confusion Matrix
actual_6 = test_set$Level_six
table(actual_6,predictions_6)
#---------------------------------------------------------------------------------------------
#Level 7
#Target Variable
Y_Target = training_set$Level_seven
#Fitting the model in Training Dataset
rf_model_7 = randomForest(x = training_set1, y= Y_Target,ntree=500)
#Predict test results
pred_rf_7 = predict(rf_model_7, newdata=test_set1)
predictions_7 = (ifelse(pred_rf_7>0.5,1,0))
#Confusion Matrix
actual_7 = test_set$Level_seven
table(actual_7,predictions_7)
#----------------------------------------------------------------------------------------------
#Level 8
#Target Variable
Y_Target = training_set$Level_eight
#Fitting the model in Training Dataset
rf_model_8 = randomForest(x = training_set1, y= Y_Target,ntree=500)
#Predict test results
pred_rf_8 = predict(rf_model_8, newdata=test_set1)
predictions_8 = (ifelse(pred_rf_8>0.5,1,0))
#Confusion Matrix
actual_8 = test_set$Level_eight
table(actual_8,predictions_8)
#-----------------------------------------------------------------------------------------------
# prediting training data (Random Forest)
#Level 0
pred_rf_train_0 = predict(rf_model, newdata=training_set1)
predictions_train_0 = (ifelse(pred_rf_train_0>0.5,1,0))
actual_train_0 <- training_set$Level_Zero
table(actual_train_0,predictions_train_0)
#Level 1
pred_rf_train_1 = predict(rf_model_1, newdata=training_set1)
predictions_train_1 = (ifelse(pred_rf_train_1>0.5,1,0))
actual_train_1 <- training_set$Level_One
table(actual_train_1,predictions_train_1)
#Level 2
pred_rf_train_2 = predict(rf_model_2, newdata=training_set1)
predictions_train_2 = (ifelse(pred_rf_train_2>0.5,1,0))
actual_train_2 <- training_set$Level_two
table(actual_train_2,predictions_train_2)
#Level 3
pred_rf_train_3 = predict(rf_model_3, newdata=training_set1)
predictions_train_3 = (ifelse(pred_rf_train_3>0.5,1,0))
actual_train_3 <- training_set$Level_three
table(actual_train_3,predictions_train_3)
#Level 4
pred_rf_train_4 = predict(rf_model_4, newdata=training_set1)
predictions_train_4 = (ifelse(pred_rf_train_4>0.5,1,0))
actual_train_4 <- training_set$Level_four
table(actual_train_4,predictions_train_4)
#Level 5
pred_rf_train_5 = predict(rf_model_5, newdata=training_set1)
predictions_train_5 = (ifelse(pred_rf_train_5>0.5,1,0))
actual_train_5 <- training_set$Level_five
table(actual_train_5,predictions_train_5)
#Level 6
pred_rf_train_6 = predict(rf_model_6, newdata=training_set1)
predictions_train_6 = (ifelse(pred_rf_train_6>0.5,1,0))
actual_train_6 <- training_set$Level_six
table(actual_train_6,predictions_train_6)
#Level 7
pred_rf_train_7 = predict(rf_model_7, newdata=training_set1)
predictions_train_7 = (ifelse(pred_rf_train_7>0.5,1,0))
actual_train_7 <- training_set$Level_seven
table(actual_train_7,predictions_train_7)
#Level 8
pred_rf_train_8 = predict(rf_model_8, newdata=training_set1)
predictions_train_8 = (ifelse(pred_rf_train_8>0.5,1,0))
actual_train_8 <- training_set$Level_eight
table(actual_train_8,predictions_train_8)
# Predicting Training Data
rf_train_pred = data.frame(predictions_train_0,predictions_train_1,predictions_train_2,predictions_train_3,predictions_train_4,predictions_train_5,predictions_train_6,predictions_train_7,predictions_train_8)
rf_train_pred_prob = data.frame(pred_rf_train_0,pred_rf_train_1,pred_rf_train_2,pred_rf_train_3,pred_rf_train_4,pred_rf_train_5,pred_rf_train_6,pred_rf_train_7,pred_rf_train_8)
# Predicting Testing Data
rf_test_pred = data.frame(predictions_0,predictions_1,predictions_2,predictions_3,predictions_4,predictions_5,predictions_6,predictions_7,predictions_8)
rf_test_pred_prob = data.frame(pred_rf_0,pred_rf_1,pred_rf_2,pred_rf_3,pred_rf_4,pred_rf_5,pred_rf_6,pred_rf_7,pred_rf_8)
# Aggregating the predictions and adding the tags
rf_tags = list()
for (i in seq(1:nrow(rf_test_pred)))
{
rf_tags[i] = list(which(rf_test_pred[i,]==1)-1)
}
rf_tags = vapply(rf_tags, paste, collapse = ", ", character(1L))
# Writing the Predicted Testing data & its probabilities to file Random_Forest_Test_Predictions
write.csv(cbind(rf_test_pred,rf_test_pred_prob,rf_tags),"C:/Users/sriram rajagopalan/Downloads/Uconn/R Proj/Results/Random_Forest_Test_Predictions.csv")
# Writing the Predicted Training data & its probabilities to file Random_Forest_Train_Predictions
#write.csv(cbind(rf_train_pred,rf_train_pred_prob),"C:/Users/sriram rajagopalan/Downloads/Uconn/R Proj/Results/Random_Forest_Train_Predictions.csv")
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#XGBoost
# Converting the training and testing data to Matrix format.
train_matrix = as.matrix(training_set1)
test_matrix = as.matrix(test_set1)
#-----------------------------------------------------------------------------------------------------------
#Level 0
#Target Variable
Y_Target = training_set$Level_Zero
#Fitting the model in Training Dataset
XG_model_0 = xgboost(data = train_matrix, label = Y_Target, nrounds = 100, objective = "binary:logistic")
#Predict test results
pred_XG_0 = predict(XG_model_0, newdata=test_matrix)
XG_predictions_0 = (ifelse(pred_XG_0>0.5,1,0))
#Confusion Matrix
XG_actual_0 = test_set$Level_Zero
table(XG_actual_0,XG_predictions_0)
#----------------------------------------------------------------------------------------------------------
#Level 1
#Target Variable
Y_Target = training_set$Level_One
#Fitting the model in Training Dataset
XG_model_1 = xgboost(data = train_matrix, label = Y_Target, nrounds = 100 , objective = "binary:logistic")
#Predict test results
pred_XG_1 = predict(XG_model_1, newdata=test_matrix)
XG_predictions_1 = (ifelse(pred_XG_1>0.5,1,0))
#Confusion Matrix
XG_actual_1 = test_set$Level_One
table(XG_actual_1,XG_predictions_1)
#---------------------------------------------------------------------------------------------------------------------
#Level 2
#Target Variable
Y_Target = training_set$Level_two
#Fitting the model in Training Dataset
XG_model_2 = xgboost(data = train_matrix, label = Y_Target, nrounds = 100 , objective = "binary:logistic")
#Predict test results
pred_XG_2 = predict(XG_model_2, newdata=test_matrix)
XG_predictions_2 = (ifelse(pred_XG_2>0.5,1,0))
#Confusion Matrix
XG_actual_2 = test_set$Level_two
table(XG_actual_2,XG_predictions_2)
#----------------------------------------------------------------------------------------------------------------------
#Level 3
#Target Variable
Y_Target = training_set$Level_three
#Fitting the model in Training Dataset
XG_model_3 = xgboost(data = train_matrix, label = Y_Target, nrounds = 100 , objective = "binary:logistic")
#Predict test results
pred_XG_3 = predict(XG_model_3, newdata=test_matrix)
XG_predictions_3 = (ifelse(pred_XG_3>0.5,1,0))
#Confusion Matrix
XG_actual_3 = test_set$Level_three
table(XG_actual_3,XG_predictions_3)
#---------------------------------------------------------------------------------------------------------------------
#Level 4
#Target Variable
Y_Target = training_set$Level_four
#Fitting the model in Training Dataset
XG_model_4 = xgboost(data = train_matrix, label = Y_Target, nrounds = 100 , objective = "binary:logistic")
#Predict test results
pred_XG_4 = predict(XG_model_4, newdata=test_matrix)
XG_predictions_4 = (ifelse(pred_XG_4>0.5,1,0))
#Confusion Matrix
XG_actual_4 = test_set$Level_four
table(XG_actual_4,XG_predictions_4)
#----------------------------------------------------------------------------------------------------------------------
#Level 5
#Target Variable
Y_Target = training_set$Level_five
#Fitting the model in Training Dataset
XG_model_5 = xgboost(data = train_matrix, label = Y_Target, nrounds = 100 , objective = "binary:logistic")
#Predict test results
pred_XG_5 = predict(XG_model_5, newdata=test_matrix)
XG_predictions_5 = (ifelse(pred_XG_5>0.5,1,0))
#Confusion Matrix
XG_actual_5 = test_set$Level_five
table(XG_actual_5,XG_predictions_5)
#--------------------------------------------------------------------------------------------------------------------------
#Level 6
#Target Variable
Y_Target = training_set$Level_six
#Fitting the model in Training Dataset
XG_model_6 = xgboost(data = train_matrix, label = Y_Target, nrounds = 100 , objective = "binary:logistic")
#Predict test results
pred_XG_6 = predict(XG_model_6, newdata=test_matrix)
XG_predictions_6 = (ifelse(pred_XG_6>0.5,1,0))
#Confusion Matrix
XG_actual_6 = test_set$Level_six
table(XG_actual_6,XG_predictions_6)
#--------------------------------------------------------------------------------------------------------------------------
#Level 7
#Target Variable
Y_Target = training_set$Level_seven
#Fitting the model in Training Dataset
XG_model_7 = xgboost(data = train_matrix, label = Y_Target, nrounds = 100 , objective = "binary:logistic")
#Predict test results
pred_XG_7 = predict(XG_model_7, newdata=test_matrix)
XG_predictions_7 = (ifelse(pred_XG_7>0.5,1,0))
#Confusion Matrix
XG_actual_7 = test_set$Level_seven
table(XG_actual_7,XG_predictions_7)
#--------------------------------------------------------------------------------------------------------------------------
#Level 8
#Target Variable
Y_Target = training_set$Level_eight
#Fitting the model in Training Dataset
XG_model_8 = xgboost(data = train_matrix, label = Y_Target, nrounds = 100 , objective = "binary:logistic")
#Predict test results
pred_XG_8 = predict(XG_model_8, newdata=test_matrix)
XG_predictions_8 = (ifelse(pred_XG_8>0.5,1,0))
#Confusion Matrix
XG_actual_8 = test_set$Level_eight
table(XG_actual_8,XG_predictions_8)
#---------------------------------------------------------------------------------------------------------------------------
XG_test_pred = data.frame(XG_predictions_0,XG_predictions_1,XG_predictions_2,XG_predictions_3,XG_predictions_4,XG_predictions_5,XG_predictions_6,XG_predictions_7,XG_predictions_8)
XG_test_pred_prob = data.frame(pred_XG_0,pred_XG_1,pred_XG_2,pred_XG_3,pred_XG_4,pred_XG_5,pred_XG_6,pred_XG_7,pred_XG_8)
XG_tags = list()
for (i in seq(1:nrow(XG_test_pred)))
{
XG_tags[i] = list(which(XG_test_pred[i,]==1)-1)
}
XG_tags = vapply(XG_tags, paste, collapse = ", ", character(1L))
write.csv(cbind(XG_test_pred,XG_test_pred_prob,XG_tags),"C:/Users/sriram rajagopalan/Downloads/Uconn/R Proj/Results/XG_test_predictions.csv")
#---------------------------------------------------------------------------------------------------------------------------
# Averaged Model: (Averaging the Probabilies of Random forest and XG Boost)
avg_prob = data.frame()
for (i in seq(1:9))
{
for (j in seq(1:nrow(XG_test_pred_prob)))
{
avg_prob[j,i] = (XG_test_pred_prob[j,i] + rf_test_pred_prob[j,i])/2
}
}
avg_pred = (ifelse(avg_prob>0.5,1,0))
colnames(avg_pred) <- c("Label_0","Label_1","Label_2","Label_3","Label_4","Label_5","Label_6","Label_7","Label_8")
avg = as.data.frame(avg_pred)
avg_model_pred = avg$Label_8
table(XG_actual_8,avg_model_pred)
avg_tags = list()
for (i in seq(1:nrow(avg_pred)))
{
avg_tags[i] = list(which(avg_pred[i,]==1)-1)
}
Predicted_tags = vapply(avg_tags, paste, collapse = ", ", character(1L))
#---------------------------------------------------------------------------------------------------------------------------
# Model Comparison
set.seed(123)
split_labels = sample.split(train$labels, SplitRatio = 0.75)
test_labels = subset(train, split == FALSE)
colnames(test_labels) <- c("Business ID","Actual Tags")
write.csv(cbind(test_labels,rf_tags,XG_tags, Predicted_tags),"C:/Users/sriram rajagopalan/Downloads/Uconn/R Proj/Results/Final_Tags.csv",row.names = F)
#----------------------------------------------------------------------------------------------------------------------------
# Final Result
write.csv(cbind(test_labels,Predicted_tags),"C:/Users/sriram rajagopalan/Downloads/Uconn/R Proj/Results/Final_Result.csv",row.names = F)
#-----------------------------------------------------------------------------------------------------------------------------------