Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Applied Supervised Learning with R

You're reading from   Applied Supervised Learning with R Use machine learning libraries of R to build models that solve business problems and predict future trends

Arrow left icon
Product type Paperback
Published in May 2019
Publisher
ISBN-13 9781838556334
Length 502 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Jojo Moolayil Jojo Moolayil
Author Profile Icon Jojo Moolayil
Jojo Moolayil
Karthik Ramasubramanian Karthik Ramasubramanian
Author Profile Icon Karthik Ramasubramanian
Karthik Ramasubramanian
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Applied Supervised Learning with R
Preface
1. R for Advanced Analytics FREE CHAPTER 2. Exploratory Analysis of Data 3. Introduction to Supervised Learning 4. Regression 5. Classification 6. Feature Selection and Dimensionality Reduction 7. Model Improvements 8. Model Deployment 9. Capstone Project - Based on Research Papers Appendix

Chapter 7: Model Improvements


Activity 12: Perform Repeated K-Fold Cross Validation and Grid Search Optimization

  1. Load the required packages mlbench, caret, and dplyr for the exercise:

    library(mlbench)
    library(dplyr)
    library(caret)
  2. Load the PimaIndianDiabetes dataset into memory from mlbench package:

    data(PimaIndiansDiabetes)
    df<-PimaIndiansDiabetes
  3. Set a seed value as 2019 for reproducibility:

    set.seed(2019)
  4. Define the K-Fold validation object using the trainControl function from the caret package and define method as repeatedcv instead of cv. Define an additional construct in the trainControl function for the number of repeats in the validation repeats = 10:

    train_control = trainControl(method = "repeatedcv",  number=5, repeats = 10,   savePredictions = TRUE,verboseIter = TRUE)
  5. Define the grid for hyperparameter mtry of random forest model as (3,4,5):

    parameter_values = expand.grid(mtry=c(3,4,5))
  6. Fit the model with the grid values, cross-validation object, and random forest classifier:

    model_rf_kfold<- train(diabetes~., data=df, trControl=train_control, method="rf",  metric= "Accuracy", tuneGrid = parameter_values)
  7. Study the model performance by printing the average accuracy and standard deviation of accuracy:

    print(paste("Average Accuracy :",mean(model_rf_kfold$resample$Accuracy)))
    print(paste("Std. Dev Accuracy :",sd(model_rf_kfold$resample$Accuracy)))
  8. Study the model performance by plotting the accuracy across different values of the hyperparameter:

    plot(model_rf_kfold)

    The final output is as follows:

    Figure 7.17: Model performance accuracy across different values of the hyperparameter

In this plot, we can see that we perform repeated k-fold cross-validation and grid search optimization on the same model.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image