########################################################################
################### ENSEMBLE MODELS ####################################
########################################################################
rm(list=ls())
#Load Library
library(h2o)
library(randomForest)
library(rpart)
require(graphics)
#Setwd
setwd("C:/Users/sinhsant/Desktop/Machine Learning with R/5. Ensemble Learning")
#INPUT
train <- [Link](file="[Link]", header = TRUE, sep = ",")
test <- [Link](file="[Link]", header = TRUE, sep = ",")
View(train)
View(test)
################################### DECISION TREE
##################################
#Model Building
rpartfit <- rpart(Status_Final~., data=train)
#Prediction with modified model
rpart_pred1 <- predict(rpartfit, test, type = "class")
#Rpart - Prediction accuracy on Test Data
table(rpart_pred1,test$Status_Final)
mean(rpart_pred1==test$Status_Final)
################################### RF with Caret
###################################
library(randomForest)
library(randomForestSRC)
library(caret)
#Build Model
modfit <- train(Status_Final~ .,method="rf",data=train)
#Training Data
pred <- predict(modfit,train)
table(pred,train$Status_Final)
mean(pred==train$Status_Final)
#Test Data
[Link]<-predict(modfit,newdata=test)
table([Link],test$Status_Final)
mean([Link]==test$Status_Final)
########################## ADABOOSTING METHOD #########################
library(adabag)
adaboost<-boosting(Status_Final~., data=train, boos=TRUE,
mfinal=150,coeflearn='Breiman')
summary(adaboost) #mfinal=20 indicates the times of repeated process is 20
#Perf on valid data
adaboost_pred1 <-predict(adaboost,test)
table(test$Status_Final,adaboost_pred1$class)
mean(test$Status_Final==adaboost_pred1$class)
########################## WITH H2O ######################################
## Create an H2O cloud
[Link](
nthreads=-1, ## -1: use all available threads
max_mem_size = "2G") ## specify the memory size for the H2O cloud
[Link]() ## Clean slate - just in case the cluster was already
running
## Load a file from disk
train <- [Link](path = normalizePath("[Link]"))
test <- [Link](path = normalizePath("[Link]"))
#Assignment within H2o
train <- [Link](train, "[Link]") #Train data: H2O name [Link]
test <- [Link](test, "[Link]") #Test data: H2O name [Link]
###################### MODEL-1: RANDOM FOREST ######################
## run our first predictive model with RF
rf1 <- [Link](
training_frame = train,
validation_frame = test,
x=2:13, ## the predictor columns, by column index
y=1, ## the target index (what we are predicting)
model_id = "rf_covType_v1", ## name the model in H2O
## not required, but helps use Flow
ntrees = 250, ## use 50-250 trees, stopping criteria will decide
finally...
stopping_rounds = 2, ## Stop fitting new trees when the 2-tree
score_each_iteration = T, ## Predict against training and validation for
each tree.
seed = 1500000)
#Performance Evaluation
summary(rf1)
rf1@model$validation_metrics ## A more direct way to access the validation
rf1@model$validation_metrics@metrics$AUC #AUC
rf1@model$validation_metrics@metrics$Gini #Gini
final_predictions_rf1<-[Link](
object = rf1,
newdata = test)
mean(final_predictions_rf1$predict==test$Status_Final) ## test set accuracy
#h2o.hit_ratio_table(rf1,valid = FALSE, xval=FALSE)[1,2]
################## A Modified RF Model ####################
rf2 <- [Link]( ##
training_frame = train, ##
validation_frame = test, ##
x=2:13, ##
y=1, ##
model_id = "rf_covType2", ##
ntrees = 250, ##
max_depth = 5, ## Increase depth, from 20
stopping_rounds = 15, ##
stopping_tolerance = 0.0001, ##
score_each_iteration = T, ##
seed=5555555) ##
#Performance Evaluation
summary(rf2)
rf2@model$validation_metrics ## A more direct way to access the validation
rf2@model$validation_metrics@metrics$AUC #AUC
rf2@model$validation_metrics@metrics$Gini #Gini
final_predictions_rf2<-[Link](
object = rf2,
newdata = test)
mean(final_predictions_rf2$predict==test$Status_Final) ## test set accuracy
######################### NOW TRY GBM MODELS #########################
#### MODEL-1: DEFAULT GBM ########
gbm1 <- [Link](
training_frame = train, ## the H2O frame for training
validation_frame = test, ## the H2O frame for validation (not required)
x=2:13, ## the predictor columns, by column index
y=1, ## the target index (what we are predicting)
model_id = "gbm_covType1", ## name the model in H2O
seed = 55005)
#Performance Evaluation
summary(gbm1)
gbm1@model$validation_metrics ## A more direct way to access the validation
gbm1@model$validation_metrics@metrics$AUC #AUC
gbm1@model$validation_metrics@metrics$Gini #Gini
final_predictions_gbm1<-[Link](
object = gbm1,
newdata = test)
mean(final_predictions_gbm1$predict==test$Status_Final) ## test set accuracy
#### MODEL-2: MODIFIED GBM ########
gbm3 <- [Link](
training_frame = train, ##
validation_frame = test, ##
x=2:13, ##
y=1, ##
ntrees = 50, ## add a few trees (from 20, though default is 50)
learn_rate = 0.15, ## increase the learning rate even further
max_depth = 3, ##
sample_rate = 0.5, ## use a random 50% of the rows to fit each tree
col_sample_rate = 0.85, ## use 85% of the columns to fit each tree
stopping_rounds = 25, ##
stopping_tolerance = 0.0005, ##
score_each_iteration = T, ##
model_id = "gbm_covType3", ##
seed = 25000000) ## ##
#Performance Evaluation
summary(gbm3)
gbm3@model$validation_metrics ## A more direct way to access the validation
gbm3@model$validation_metrics@metrics$AUC #AUC
gbm3@model$validation_metrics@metrics$Gini #Gini
final_predictions_gbm3<-[Link](
object = gbm3,
newdata = test)
mean(final_predictions_gbm3$predict==test$Status_Final) ## test set accuracy
### All done, shutdown H2O
[Link](prompt=FALSE)
############################### KEEP PRACTICING !! ###########################