Master advanced hyperparameter tuning with RandomizedSearchCV and Bayesian optimization. Learn to scale your experiments efficiently for better ML models.
Previously in this course, we covered the basics of Hyperparameter Tuning Basics: Controlling Model Behavior and implemented a brute-force approach in Implementing Grid Search: Automating Hyperparameter Tuning. While those methods are reliable, they scale poorly as your model complexity grows. This lesson introduces more efficient strategies to explore your model's configuration space.
When you use GridSearchCV, you force the machine to evaluate every possible combination of parameters you provide. If you have 5 hyperparameters with 5 values each, that’s $5^5 = 3,125$ combinations. If you add cross-validation (say, 5 folds), you are training the model 15,625 times.
In production environments, this is often unsustainable. You need strategies that either sample the space intelligently or learn from previous trials to prune the search.
Instead of checking every point, RandomizedSearchCV samples a fixed number of parameter settings from specified distributions. Mathematically, it's often more efficient because it doesn't waste time evaluating regions of the parameter space that are unlikely to yield improvements.
PYTHONfrom sklearn.model_selection import RandomizedSearchCV from sklearn.ensemble import RandomForestClassifier from scipy.stats import randint # Define the parameter distribution param_dist = { CE9178">'n_estimators': randint(50, 500), CE9178">'max_depth': [None, 10, 20, 30], CE9178">'min_samples_split': randint(2, 11) } rf = RandomForestClassifier() # n_iter controls how many combinations are sampled random_search = RandomizedSearchCV( rf, param_distributions=param_dist, n_iter=20, cv=5, n_jobs=-1 ) random_search.fit(X_train, y_train) print(f"Best params: {random_search.best_params_}")
By setting n_iter=20, we limit the total training runs to 100 (20 iterations * 5 folds), regardless of how large the parameter grid is.
While randomized search is faster, it is "blind"—it doesn't remember that a certain parameter value performed poorly in a previous iteration. Bayesian optimization treats hyperparameter tuning as a regression problem. It builds a surrogate model (usually a Gaussian Process) to predict the performance of unseen parameter combinations based on the results of past ones.
This allows the search to spend more time in "promising" areas of the configuration space. We typically use the scikit-optimize (skopt) library for this.
PYTHONfrom skopt import BayesSearchCV from skopt.space import Real, Integer # Define the search space search_space = { CE9178">'n_estimators': Integer(50, 500), CE9178">'max_depth': Integer(1, 30), CE9178">'min_samples_split': Real(0.01, 0.1) # Supports continuous ranges } opt = BayesSearchCV( RandomForestClassifier(), search_space, n_iter=32, cv=3 ) opt.fit(X_train, y_train) print(f"Best score found: {opt.best_score_}")
pip install scikit-optimize.GridSearchCV with BayesSearchCV.n_iter for the surrogate model to "learn" the landscape. If you set it too low, it performs no better than random guessing.We've moved from exhaustive grid search to smarter, more efficient exploration methods. RandomizedSearchCV provides a quick way to sample large spaces, while Bayesian optimization uses historical data to guide the search toward optimal configurations. By leveraging these tools, you can maintain high model performance while keeping your experimentation cycles short.
Up next: We will discuss how to keep your models healthy in production by learning about Model Monitoring in Practice.
Master feature selection with RFECV. Learn how to automate the removal of noisy, irrelevant features to build simpler, more robust machine learning models.
Read moreLearn to use GridSearchCV to automate hyperparameter tuning. Master the art of defining parameter grids and extracting the best model settings for your project.
Advanced Hyperparameter Search