Get startedGet started for free

Custom tuning grids

1. Custom tuning grids

In the last video, we learned how to use the tuneLength argument to customize caret models. However, we're not limited to the defaults train chooses for us.

2. Pros and cons of custom tuning

We can pass our own, fully-customized grids as data.frames to the tuneGrid argument in train function. This is the most flexible method of fitting and tuning caret models, and gives us complete control over the models that are explored during grid search. The major drawback of this method is that it requires the most knowledge of the how the model works, and can dramatically increase the model's runtime if you use a very large tuning grid. However, it also gives you full control over the model you are tuning, and if you know the algorithm well, you can exploit that knowledge to get better results from your models.

3. Custom tuning example

Lets make a custom tuning grid. To start, we need to make a data.frame with the values of tuning parameters we want to explore. Random forests have a single tuning parameter (mtry), so we make a data.frame with a single column. In the last video, we saw that mtry values of 2, 8, and 14 did well, so we'll make a grid that explores the lower portion of the tuning space in more detail, looking at 2,3,4 and 5, as well as 10 and 20 as values for mtry. After fitting our model, we can plot the results.

4. Custom tuning

In this case the tuning value of 10 looks to be the best, though perhaps not quite as good as the 14 caret chose by default in the previous video.

5. Let's practice!

Let's explore some custom tuning grids on other datasets.