Challenge: Tuning Hyperparameters with RandomizedSearchCV
The principle of RandomizedSearchCV
is similar to GridSearchCV
, but instead of testing every possible combination, it evaluates only a randomly sampled subset.
For instance, the following param_grid
contains 100 combinations:
param_grid = {
'n_neighbors': [1, 3, 5, 7, 9, 12, 15, 17, 20, 25],
'weights': ['distance', 'uniform'],
'p': [1, 2, 3, 4, 5]
}
GridSearchCV
would test all 100, which is time-consuming. RandomizedSearchCV
can instead evaluate a smaller subset, e.g., 20 randomly chosen combinations. This reduces computation time and usually produces results close to the best.
The number of combinations to test is controlled by the n_iter
argument (default is 10). Other than that, usage is the same as with GridSearchCV
.
Swipe to start coding
- Initialize a
RandomizedSearchCV
object with the parameter grid and setn_iter=20
. - Initialize a
GridSearchCV
object with the same parameter grid. - Train both search objects using
.fit(X, y)
. - Print the best estimator from the grid search with
.best_estimator_
. - Print the best score from the randomized search with
.best_score_
.
Solution
You can try running the code several times. Look at the difference between the two scores. Sometimes the scores can be the same due to the presence of the best parameters among combinations sampled by RandomizedSearchCV
.
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain when to use RandomizedSearchCV instead of GridSearchCV?
How do I choose the right value for n_iter in RandomizedSearchCV?
What are the main advantages and disadvantages of RandomizedSearchCV?
Awesome!
Completion rate improved to 3.13
Challenge: Tuning Hyperparameters with RandomizedSearchCV
Swipe to show menu
The principle of RandomizedSearchCV
is similar to GridSearchCV
, but instead of testing every possible combination, it evaluates only a randomly sampled subset.
For instance, the following param_grid
contains 100 combinations:
param_grid = {
'n_neighbors': [1, 3, 5, 7, 9, 12, 15, 17, 20, 25],
'weights': ['distance', 'uniform'],
'p': [1, 2, 3, 4, 5]
}
GridSearchCV
would test all 100, which is time-consuming. RandomizedSearchCV
can instead evaluate a smaller subset, e.g., 20 randomly chosen combinations. This reduces computation time and usually produces results close to the best.
The number of combinations to test is controlled by the n_iter
argument (default is 10). Other than that, usage is the same as with GridSearchCV
.
Swipe to start coding
- Initialize a
RandomizedSearchCV
object with the parameter grid and setn_iter=20
. - Initialize a
GridSearchCV
object with the same parameter grid. - Train both search objects using
.fit(X, y)
. - Print the best estimator from the grid search with
.best_estimator_
. - Print the best score from the randomized search with
.best_score_
.
Solution
You can try running the code several times. Look at the difference between the two scores. Sometimes the scores can be the same due to the presence of the best parameters among combinations sampled by RandomizedSearchCV
.
Thanks for your feedback!
Awesome!
Completion rate improved to 3.13single