-
Notifications
You must be signed in to change notification settings - Fork 18
/
DIY_ensembling.R
141 lines (111 loc) · 5.75 KB
/
DIY_ensembling.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
library(data.table)
library(mlr)
path<- "[your folder]"
setwd(path)
data <- fread("loan_data.csv", na.strings = c("", " ", "?", "NA", NA))
summarizeColumns(data)
# seperate data into numericla and characterical
num_cols <- sapply(data, is.numeric)
num_data <- subset(data, select = num_cols==T)
fact_data <- subset(data, select = num_cols==F)
# impute the missing data in numerical data
summarizeColumns(num_data)
library(caret)
library('RANN')
set.seed(410)
imputed_num_cols <- preProcess(num_data, method = c("medianImpute","center","scale"))
imputed_num_data <- predict(imputed_num_cols, num_data)
summarizeColumns(imputed_num_data)
# impute the missing data in categorical data, as "MISSING"
summarizeColumns(fact_data)
for (i in seq_along(fact_data)) set(fact_data, i=which(is.na(fact_data[[i]])), j=i, value="MISSING")
summarizeColumns(fact_data)
imputed_data <- cbind(imputed_num_data, fact_data)
summarizeColumns(imputed_data)
# split into training and testing data, select specific cols
idx <- createDataPartition(imputed_data$Loan_Status, p=0.7, list=F)
trCols <- c("Credit_History", "LoanAmount", "Loan_Amount_Term", "ApplicantIncome", "CoapplicantIncome", "Loan_Status")
train_set <- imputed_data[idx, c(1:5, 13), with=F]
test_set <- imputed_data[-idx, c(1:5, 13), with=F]
train_task <- makeClassifTask(data=data.frame(train_set), target = "Loan_Status", positive = "Y")
test_task <- makeClassifTask(data=data.frame(test_set), target = "Loan_Status", positive = "Y")
# show all classifications can be used in mlr
listLearners("classif", check.packages = F)[c("class","package")]
# train multiple models, these are the bottom models in ensembling
## Random Forest
set.seed(410)
getParamSet("classif.randomForest")
rf_learner <- makeLearner("classif.randomForest", predict.type = "prob", par.vals = list(ntree = 200, mtry = 3))
rf_learner$par.vals <- list(importance = TRUE)
rf_param <- makeParamSet(
makeIntegerParam("ntree",lower = 50, upper = 500),
makeIntegerParam("mtry", lower = 3, upper = 10),
makeIntegerParam("nodesize", lower = 10, upper = 50)
)
rancontrol <- makeTuneControlRandom(maxit = 50L)
cv_rf <- makeResampleDesc("CV",iters = 5L)
rf_tune <- tuneParams(learner = rf_learner, resampling = cv_rf, task = train_task, par.set = rf_param, control = rancontrol, measures = acc)
rf_tune$x
rf_tune$y
rf.tree <- setHyperPars(rf_learner, par.vals = rf_tune$x)
rf_model <- mlr::train(learner=rf.tree, task=train_task)
getLearnerModel(rf_model)
rfpredict <- predict(rf_model, test_task)
nb_prediction <- rfpredict$data$response
dCM <- confusionMatrix(test_set$Loan_Status, nb_prediction, positive = "Y")
dCM
## KNN
fitControl <- trainControl(
method = "cv",
number = 5,
savePredictions = 'final',
classProbs = T)
train_set$Loan_Status <- as.factor(train_set$Loan_Status)
test_set$Loan_Status <- as.factor(test_set$Loan_Status)
set.seed(410)
model_knn<-train(train_set[,c(1:5), with=F],train_set$Loan_Status,method='knn',trControl=fitControl,tuneLength=3)
knn_predict <- predict(object=model_knn, test_set[,c(1:5), with=F], type="prob")
## logistics regression
set.seed(410)
model_lr<-train(train_set[,c(1:5), with=F],train_set$Loan_Status,method='glm',trControl=fitControl,tuneLength=3)
lr_predict <- predict(object=model_lr, test_set[,c(1:5), with=F], type="prob")
# DIY Ensembling - Averaging Scores, have to use probability output
pred_avg <- (rfpredict$data$prob.Y + knn_predict$Y + lr_predict$Y)/3
## split into response output
test_set[, avg_pred := data.frame(as.factor(ifelse(pred_avg>0.5, 'Y', 'N')))]
head(test_set)
# DIY Ensembling - Majority Votes
test_set[, pred_rf:= rfpredict$data$response]
test_set[, pred_lr := predict(object=model_lr, test_set[,c(1:5), with=F])]
test_set[, pred_knn := predict(object=model_knn, test_set[,c(1:5), with=F])]
test_set[, pred_rf := rfpredict$data$response]
head(test_set)
test_set[, majority_pred:=data.frame(as.factor(ifelse(pred_rf=='Y' & pred_lr=='Y', 'Y', ifelse(pred_rf=='Y' & pred_knn=='Y', 'Y', ifelse(pred_knn=='Y' & pred_lr=='Y', 'Y', 'N')))))]
head(test_set)
# DIY Ensembling - Weighted Average, have to use probability output
pred_weighted_avg <- (rfpredict$data$prob.Y*0.5 + knn_predict$Y*0.25 + lr_predict$Y*0.25)
## split into response output
test_set[, weighted_avg_pred := data.frame(as.factor(ifelse(pred_weighted_avg>0.5, 'Y', 'N')))]
head(test_set)
# NOTE: if the prediction resultes of these bottom models are highly correlated, using them together may not bring better results
cor(data.frame(as.numeric(test_set$pred_knn), as.numeric(test_set$pred_lr), as.numeric(test_set$pred_rf)))
# DIY Stacking
## base models training
model_rf<-train(train_set[,c(1:5), with=F],train_set$Loan_Status,method='rf',trControl=fitControl,tuneLength=3)
model_lr<-train(train_set[,c(1:5), with=F],train_set$Loan_Status,method='glm',trControl=fitControl,tuneLength=3)
model_knn<-train(train_set[,c(1:5), with=F],train_set$Loan_Status,method='knn',trControl=fitControl,tuneLength=3)
##Predicting the out of fold prediction probabilities for training data
train_set$OOF_pred_rf<-model_rf$pred$Y[order(model_rf$pred$rowIndex)]
train_set$OOF_pred_knn<-model_knn$pred$Y[order(model_knn$pred$rowIndex)]
train_set$OOF_pred_lr<-model_lr$pred$Y[order(model_lr$pred$rowIndex)]
##Predicting probabilities for the test data
test_set$OOF_pred_rf<-predict(model_rf,test_set[,c(1:5), with=F],type='prob')$Y
test_set$OOF_pred_knn<-predict(model_knn,test_set[,c(1:5), with=F],type='prob')$Y
test_set$OOF_pred_lr<-predict(model_lr,test_set[,c(1:5), with=F],type='prob')$Y
## independent variables for the top layer
head(train_set)
predictors_top <- c(7:9)
model_gbm <- train(train_set[,c(1:5), with=F],train_set$Loan_Status,method='gbm',trControl=fitControl,tuneLength=3)
## predict using GBM top layer model
test_set$gbm_stacked<-predict(model_gbm,test_set[,predictors_top, with=F])
head(test_set)