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
You are given a preprocessed penguin dataset ready for model training.
Your goal is to tune the hyperparameters of a KNeighborsClassifier model using both grid search and randomized search methods.
- Define the parameter grid named
param_gridwith the desired values forn_neighbors,weights, andp. - Initialize a
RandomizedSearchCVobject using the defined parameter grid, setn_iter=20. - Initialize a
GridSearchCVobject using the same parameter grid. - Train both search objects on the dataset using the
.fit(X, y)method. - Print the best estimator from the grid search using
.best_estimator_. - Print the best cross-validation score from the randomized search using
.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
You are given a preprocessed penguin dataset ready for model training.
Your goal is to tune the hyperparameters of a KNeighborsClassifier model using both grid search and randomized search methods.
- Define the parameter grid named
param_gridwith the desired values forn_neighbors,weights, andp. - Initialize a
RandomizedSearchCVobject using the defined parameter grid, setn_iter=20. - Initialize a
GridSearchCVobject using the same parameter grid. - Train both search objects on the dataset using the
.fit(X, y)method. - Print the best estimator from the grid search using
.best_estimator_. - Print the best cross-validation score from the randomized search using
.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