R Tutorial: Hyperparameter tuning in caret
Skills:
Fine-tuning LLMs80%
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
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
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
More on: Fine-tuning LLMs
View skill →Related Reads
📰
📰
📰
📰
DeepSeek V4 API Pricing Explained: Pro vs Flash, and How to Call Both With One Key
Dev.to AI
What the VPA Recommender Is Actually Computing (And Why It Disagrees With You)
Dev.to · Prasad MK
AI Watermark Remover: How EzMaker AI Turns Messy Images into Usable Assets
Dev.to · Lily Chen
AI APIs in 2026: The Honest Developer's Guide to Choosing One
Dev.to · Shaw Sha
🎓
Tutor Explanation
DeepCamp AI