Support vector machine bagging (SVMBag) implementation

The steps of loading the libraries, registering multiprocessing, setting a working directory, reading data from a working directory, removing nondiscriminatory features from data, and setting up cross-validation parameters remain the same in the SVMBag and NBBag implementations as well. So, we do not repeat these steps in the SVMBag or NBBag code. Rather, we will focus on discussing the SVMBag or NBBag specific code:

# Setting up SVM predict function as the default svmBag$pred function has some code issue 
svm.predict <- function (object, x)
{
if (is.character(lev(object))) {
out <- predict(object, as.matrix(x), type = "probabilities")
colnames(out) <- lev(object)
rownames(out) <- NULL
}
else out <- predict(object, as.matrix(x))[, 1]
out
}
# setting up parameters to build svm bagging model
bagctrl <- bagControl(fit = svmBag$fit,
predict = svm.predict ,
aggregate = svmBag$aggregate)
# fit the bagged svm model
set.seed(300)
svmbag <- train(Attrition ~ ., data = mydata, method="bag",trControl = cvcontrol, bagControl = bagctrl,allowParallel = TRUE)
# printing the model results
svmbag

This will result in the following output:

Bagged Model  

1470 samples
30 predictors
2 classes: 'No', 'Yes'

No pre-processing
Resampling: Cross-Validated (10 fold, repeated 10 times)
Summary of sample sizes: 1324, 1324, 1323, 1323, 1323, 1323, ...
Resampling results:
Accuracy Kappa
0.8777721 0.4749657

Tuning parameter 'vars' was held constant at a value of 44

You will see that we achieved an accuracy of 87.7%, which is much higher than the KNN model's 84% accuracy.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset