Grid or random search? You might be using the wrong one


Welcome!

Every Monday, I’ll drop a no-fluff, straight-to-the-point tip on a data science skill, tool, or
method to help you stay sharp in the field. I hope you find it useful!


Coming soon: Master Tree Ensemble Models. From Decision Trees to Gradient Boosting!

We’re developing a comprehensive new course on the models that dominate machine learning for tabular data, starting with decision trees and progressing through powerful ensemble methods.

The main focus will be on the all-time favourites: XGBoost, LightGBM, and CatBoost. You’ll discover how they work, what makes each library different, and how to choose and use the right model for your projects.

Stay tuned—more details are coming soon!


Grid or random search? You might be using the wrong one

Are you using grid search when you need a continuous distribution? Passing a grid to random search? Running a long random search for just two or three hyperparameters?

Your search may be doing more work while exploring fewer useful values than you intended.

Are you using grid search for a continuous range?

Grid search evaluates every combination of the values you specify. Two hyperparameters with three values each give nine combinations. For example, if your grid is:

param_grid = {

"max_depth": [2, 3, 4],

"n_estimators": [100, 500, 1000],

}

Grid search will train and evaluate nine models: (2, 100), (2, 500), (2, 1000), (3, 100), (3, 500), (3, 1000), (4, 100), (4, 500), (4, 1000).

But here's the thing: a grid tests only the values you choose. It cannot explore what lies between them. For an integer hyperparameter with a small useful range, like max_depth, that's usually fine. But for n_estimators, we never test anything between 100 and 500, or between 500 and 1000, and the best value may sit there.

The problem gets worse for hyperparameters that are continuous by nature, such as the learning rate or a regularization coefficient. For these, consider random search and define the search space with continuous distributions instead of lists. That way you can explore many more values within your chosen ranges:

from scipy.stats import loguniform, randint, uniform

param_distributions = {

"max_depth": [3, 4, 5],

"n_estimators": randint(100, 1000),

"learning_rate": loguniform(1e-3, 0.3),

"subsample": uniform(0.5, 0.5), # from 0.5 to 1.0

}

Now n_estimators can take any integer between 100 and 999, learning_rate any value between 0.001 and 0.3 sampled on a log scale, and subsample any value between 0.5 and 1.0. Each candidate is a fresh draw, so with n_iter=30 you test 30 different learning rates rather than the three you would have hand-picked for a grid.

Are you passing a grid to random search?

Then you are still limited to the grid points. You lose the benefit of exploring values between them.

Multiply the number of values per hyperparameter to get the total number of combinations, then compare it with n_iter. If n_iter is equal to or larger than that total, scikit-learn caps the search at the grid size, and you are running grid search with extra steps. Use GridSearchCV instead. If you only want a subset of the combinations, set n_iter below the total.

Are you using random search for two or three hyperparameters?

With only a few candidate values, a small grid is often the simpler choice. If your hyperparameters are continuous, however, and you don’t know where the optimal value lies, then you need to set up a distribution.

Look at the parameter n_iter. For 2-3 hyperparameters, you may not need to train 50 models, with 20 you may already find the optimal values and save a lot of time.

Could you speed up your searches?

If you are running random search on a lot of hyperparameters, you could speed the search dramatically by using successive halving.

Successive halving starts candidates with a small resource budget, discards weaker performers, and gives the survivors more resources. Like this, while training many configurations, the weaker are stopped earlier, and you save time and computing resources.

Want to learn how to put it into practice? Check out my new course.

I hope this information was useful!

Wishing you a successful week ahead - see you next Monday! 👋🏻

Sole


Ready to enhance your skills?

Our specializations, courses and books are here to assist you:


Hi…I’m Sole
​
I’m a Python developer, AI educator, and developer advocate. I’m the creator and maintainer of Feature-engine and the main instructor at Train in Data.
​
I share practical Python lessons, open-source insights, and ideas to help our community build better software and grow as developers and data scientists.

You are receiving this email because you subscribed to our newsletter, signed up on our website, purchased or downloaded any products from us.


Follow us on social media

Copyright (C) 2026 Train in Data. All rights reserved.

​
​Unsubscribe · Preferences​

Train in Data

by Soledad Galli, PhD | Data scientist | Python Developer | Best-selling instructor | Book author | 👉 www.trainindata.com

Read more from Train in Data
After months with Claude and ChatGPT, here is where each one wins

Welcome! Every Monday, I’ll drop a no-fluff, straight-to-the-point tip on a data science skill, tool, ormethod to help you stay sharp in the field. I hope you find it useful! Our new course is out! Master Tree Ensemble Models: from Decision Trees to Gradient Boosting! XGBoost, LightGBM, and CatBoost are everywhere, and generative AI can now set them up in minutes. But can you explain what they are doing, why they make certain decisions, or whether the generated configuration makes sense?...

Which hyperparameters should you tune?

Welcome! Every Monday, I’ll drop a no-fluff, straight-to-the-point tip on a data science skill, tool, ormethod to help you stay sharp in the field. I hope you find it useful! Which hyperparameters should you tune? Anyone who opens the XGBoost documentation for the first time finds a long list of hyperparameters, from the learning rate and the depth of the trees to regularisation terms, sampling ratios and a few options whose purpose is far from obvious. Faced with that list, it is hard to...

Job descriptions rarely tell the whole story

Welcome! Every Monday, I’ll drop a no-fluff, straight-to-the-point tip on a data science skill, tool, ormethod to help you stay sharp in the field. I hope you find it useful! New set of focused ML interpretability courses now on Udemy I know many of you already use Udemy, so this October I released a new series of short, focused courses designed to help you learn one valuable machine learning skill at a time The Complete SHAP and Shapley Values Masterclass Interpretable Machine Learning:...