R Tutorial: Hyperparameter tuning in caret

DataCamp · Beginner ·🛠️ AI Tools & Apps ·6y ago

Key Takeaways

Performs hyperparameter tuning in R using caret package

Full Transcript

carrot makes hyper parameter tuning very easy by default it performs automatic tuning for you with every training run but you can also manually find how you want to tune your models here you see the random forest model with the RF method from before in the output we see that we only have one hyper parameter to tune em try defines the number of variables that are randomly sampled as candidates at each split carrot automatically tried three different my values and includes the performance of each with the output the best model is chosen with the metric accuracy which in this case was 4m try equals six different algorithms have different hyper parameters you might be wondering how you would know which high parameters you can tune with these different methods in carrot if you know the model abbreviation you can use the model lookup function but the easiest way is to use the online documentation of carrot click this link on the slide to go to the page there you will find an overview of the different algorithms you can set as method in the trade function this table includes the name of the model the string you need to put into the train function whether it can be used for classification or regression and what the original are package of the implementation is but most importantly you will find which hyper parameters can be tuned here I will not discuss the mathematics behind hyper parameters instead I will focus on how to perform the high parameter tuning let's change things up a bit and build a support vector machine with polynomial kernel similar to the random forest model from before this time I am using the SVM poly method and I am again calculating the training time when we examined the model object again we see that this time carrot performed a more complex hyper parameter tuning if we have more than one hyper parameter to tune train automatically creates a grid of tuning parameters by default carrot tries all possible combinations of three hyper parameters in our model degree being one two or three scale being 0.01 Oh point O 1 or 0.1 and C being 0.25 0.5 or 1 because the output shows the performance for every possible combination of Hiva parameters the output is too long to fit on this night and I am only showing the best model with degree of 1 scale of 0.1 and C equal to 1 we can also set the option tune lengths to specify the number of different values to try for each hyper parameter for example 5 now carat tries all possible combinations of 5 5 parameters degree B 1 2 3 4 or 5 scale being 1 to the power of minus 3 1 to the power of minus 2 1 to the power of minus 1 1 and 10 and C being 0.25 0.5 1 2 or 4 the best model now has degree scale and C of 1 of course you could also manually try out different hyper parameters this we can do with the option to in grid 2 which we can feed a grid of hyper parameters this grade is defined with the expanded grade function if we use that function we need to define all height parameters let's see what happens if we set the degree to 4 and keep scale and see it 1 and 3 train the model this time we only train with 1 combination of 1/2 parameters so our output gives the performance for these hyper parameters only now's your turn to apply simple height parameter

Original Description

Want to learn more? Take the full course at https://learn.datacamp.com/courses/hyperparameter-tuning-in-r at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work. --- Caret makes hyperparameter tuning very easy. By default, it performs automatic tuning for you with every training run. But you can also manually define how you want to tune your models. Here you see the random forest model with the `rf` method from before. In the output we see that we only have one hyperparameter to tune: `mtry` defines the number of variables that are randomly sampled as candidates at each split. caret automatically tried three different `mtry` values and includes the performance of each with the output. The best model is chosen with the metric `accuracy`, which in this case was for mtry = 6. Different algorithms have different hyperparameters. You might be wondering how you would know which hyperparameters you can tune with these different `method` in caret. If you know the model abbreviation, you can use the modellookup function. But the easiest way is to use the online documentation for caret. Click this link on the slides to go to the page. There, you will find an overview of the different algorithms you can set as `method` in the train function. This table includes the name of the model, the string you need to put into the train function, whether it can be used for classification or regression and what the original R package of the implementation is. But most importantly, you will find which hyperparameters can be tuned. Here, I will not discuss the mathematics behind hyperparameters, instead, I will focus on HOW to perform the hyperparameter tuning. Let's change things up a bit and build a Support Vector Machine with Polynomial Kernel similar to the Random Forest model from before: this time I am using the `svmPoly` model. And I am again calculating the training time. When we examine the model object again,
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from DataCamp · DataCamp · 0 of 60

← Previous Next →
1 SQL Server Tutorial: Date manipulation
SQL Server Tutorial: Date manipulation
DataCamp
2 R Tutorial: Intermediate Interactive Data Visualization with plotly in R
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
3 R Tutorial: Adding aesthetics to represent a variable
R Tutorial: Adding aesthetics to represent a variable
DataCamp
4 R Tutorial: Moving Beyond Simple Interactivity
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
5 Python Tutorial: Why use ML for marketing? Strategies and use cases
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
6 Python Tutorial: Preparation for modeling
Python Tutorial: Preparation for modeling
DataCamp
7 Python Tutorial: Machine Learning modeling steps
Python Tutorial: Machine Learning modeling steps
DataCamp
8 R Tutorial: The prior model
R Tutorial: The prior model
DataCamp
9 R Tutorial: Data & the likelihood
R Tutorial: Data & the likelihood
DataCamp
10 R Tutorial: The posterior model
R Tutorial: The posterior model
DataCamp
11 R Tutorial: An Introduction to plotly
R Tutorial: An Introduction to plotly
DataCamp
12 R Tutorial: Plotting a single variable
R Tutorial: Plotting a single variable
DataCamp
13 R Tutorial: Bivariate graphics
R Tutorial: Bivariate graphics
DataCamp
14 Python Tutorial: Customer Segmentation in Python
Python Tutorial: Customer Segmentation in Python
DataCamp
15 Python Tutorial: Time cohorts
Python Tutorial: Time cohorts
DataCamp
16 Python Tutorial: Calculate cohort metrics
Python Tutorial: Calculate cohort metrics
DataCamp
17 Python Tutorial: Cohort analysis visualization
Python Tutorial: Cohort analysis visualization
DataCamp
18 R Tutorial: Building Dashboards with flexdashboard
R Tutorial: Building Dashboards with flexdashboard
DataCamp
19 R Tutorial: Anatomy of a flexdashboard
R Tutorial: Anatomy of a flexdashboard
DataCamp
20 R Tutorial: Layout basics
R Tutorial: Layout basics
DataCamp
21 R Tutorial: Advanced layouts
R Tutorial: Advanced layouts
DataCamp
22 Python Tutorial: Time Series Analysis in Python
Python Tutorial: Time Series Analysis in Python
DataCamp
23 Python Tutorial: Correlation of Two Time Series
Python Tutorial: Correlation of Two Time Series
DataCamp
24 Python Tutorial: Simple Linear Regressions
Python Tutorial: Simple Linear Regressions
DataCamp
25 Python Tutorial: Autocorrelation
Python Tutorial: Autocorrelation
DataCamp
26 R Tutorial: The gapminder dataset
R Tutorial: The gapminder dataset
DataCamp
27 R Tutorial: The filter verb
R Tutorial: The filter verb
DataCamp
28 R Tutorial: The arrange verb
R Tutorial: The arrange verb
DataCamp
29 R Tutorial: The mutate verb
R Tutorial: The mutate verb
DataCamp
30 R Tutorial: What is cluster analysis?
R Tutorial: What is cluster analysis?
DataCamp
31 R Tutorial: Distance between two observations
R Tutorial: Distance between two observations
DataCamp
32 R Tutorial: The importance of scale
R Tutorial: The importance of scale
DataCamp
33 R Tutorial: Measuring distance for categorical data
R Tutorial: Measuring distance for categorical data
DataCamp
34 Python Tutorial: Plotting multiple graphs
Python Tutorial: Plotting multiple graphs
DataCamp
35 Python Tutorial: Customizing axes
Python Tutorial: Customizing axes
DataCamp
36 Python Tutorial: Legends, annotations, & styles
Python Tutorial: Legends, annotations, & styles
DataCamp
37 Python Tutorial: Introduction to iterators
Python Tutorial: Introduction to iterators
DataCamp
38 Python Tutorial: Playing with iterators
Python Tutorial: Playing with iterators
DataCamp
39 Python Tutorial: Using iterators to load large files into memory
Python Tutorial: Using iterators to load large files into memory
DataCamp
40 SQL Tutorial: Introduction to Relational Databases in SQL
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
41 SQL Tutorial: Tables: At the core of every database
SQL Tutorial: Tables: At the core of every database
DataCamp
42 SQL Tutorial: Update your database as the structure changes
SQL Tutorial: Update your database as the structure changes
DataCamp
43 Python Tutorial: Classification-Tree Learning
Python Tutorial: Classification-Tree Learning
DataCamp
44 Python Tutorial: Decision-Tree for Classification
Python Tutorial: Decision-Tree for Classification
DataCamp
45 Python Tutorial: Decision-Tree for Regression
Python Tutorial: Decision-Tree for Regression
DataCamp
46 Python Tutorial: Census Subject Tables
Python Tutorial: Census Subject Tables
DataCamp
47 Python Tutorial: Census Geography
Python Tutorial: Census Geography
DataCamp
48 Python Tutorial: Using the Census API
Python Tutorial: Using the Census API
DataCamp
49 R Tutorial: A/B Testing in R
R Tutorial: A/B Testing in R
DataCamp
50 R Tutorial: Baseline Conversion Rates
R Tutorial: Baseline Conversion Rates
DataCamp
51 R Tutorial: Designing an Experiment - Power Analysis
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
52 R Tutorial: Introduction to qualitative data
R Tutorial: Introduction to qualitative data
DataCamp
53 R Tutorial: Understanding your qualitative variables
R Tutorial: Understanding your qualitative variables
DataCamp
54 R Tutorial: Making Better Plots
R Tutorial: Making Better Plots
DataCamp
55 SQL Tutorial: OLTP and OLAP
SQL Tutorial: OLTP and OLAP
DataCamp
56 SQL Tutorial: Storing data
SQL Tutorial: Storing data
DataCamp
57 SQL Tutorial: Database design
SQL Tutorial: Database design
DataCamp
58 Python Tutorial: Introduction to spaCy
Python Tutorial: Introduction to spaCy
DataCamp
59 Python Tutorial: Statistical Models
Python Tutorial: Statistical Models
DataCamp
60 Python Tutorial: Rule-based Matching
Python Tutorial: Rule-based Matching
DataCamp

Related Reads

📰
DeepSeek V4 API Pricing Explained: Pro vs Flash, and How to Call Both With One Key
Learn how to optimize DeepSeek V4 API usage by understanding Pro vs Flash pricing and using a single key for both variants
Dev.to AI
📰
What the VPA Recommender Is Actually Computing (And Why It Disagrees With You)
Understand what the VPA recommender computes and why it may disagree with you, to improve your usage and configuration of VPA
Dev.to · Prasad MK
📰
AI Watermark Remover: How EzMaker AI Turns Messy Images into Usable Assets
Learn how EzMaker AI removes watermarks from images, enabling businesses to reuse and repurpose visual assets efficiently
Dev.to · Lily Chen
📰
AI APIs in 2026: The Honest Developer's Guide to Choosing One
Learn how to choose the right AI API for your project by considering tradeoffs, not just picking the 'best' model
Dev.to · Shaw Sha
Up next
15 NotebookLM Hacks That Change How You Work
SCALER
Watch →