Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 70

Hyperparameter Tuning with Grid and Random Search

Recommended resources

Welcome to the final module of our course! Over the past several weeks, you've built an impressive toolkit of machine learning algorithms, from linear models to complex ensembles and clustering techniques. In our last lesson, we explored t-SNE and saw how its perplexity parameter could drastically change the resulting visualization. This is just one example of a "hyperparameter"—a setting that we, as developers, must choose to guide the learning process.

So far, we have mostly used the default values for these settings. But how do we know if K=5 is truly the best choice for a KNN model, or if a learning_rate of 0.1 is optimal for a Gradient Boosting machine? Simply guessing or manually testing a few values is inefficient and unlikely to yield the best results.

This lesson marks a crucial step toward building production-ready models. We will formalize the process of finding the optimal settings for your models. Your learning outcome is to perform hyperparameter tuning for a given model using grid search and random search with cross-validation. We'll explore these two fundamental techniques, understanding how they systematically search for the best combination of hyperparameters to maximize your model's performance.

What are Hyperparameters, Really?

Before we start searching for optimal values, let's solidify the distinction between parameters and hyperparameters. Your strong background in algorithms and software development will make this distinction intuitive.

  • Parameters are values that a model learns on its own from the training data. Think of the coefficients in a linear regression model or the weights in a neural network. The learning algorithm's job is to find the optimal values for these parameters.
  • Hyperparameters are configuration settings that are external to the model and cannot be learned from data. They are the "knobs" you tune before the training process begins. Examples we've seen include the alpha regularization strength in Ridge regression, the kernel type in an SVM, or the n_estimators (number of trees) in a Random Forest.

The following article provides a clear introduction to this concept and the overall goal of hyperparameter optimization.

Hyperparameter Optimization With Random Search and ...

This article from Machine Learning Mastery will serve as our primary guide for this lesson. It clearly defines the concepts and provides excellent code examples.

Start by reading the introduction, from the beginning down to the tutorial overview. Then, continue with the section Model Hyperparameter Optimization to understand the difference between parameters and hyperparameters, and the concept of a "search space."


The Naive Approach and the Need for Automation

Imagine you want to tune an SVM classifier. You need to choose values for C, kernel, and gamma. You could write a set of nested for loops to iterate through some possible values, train a model for each combination, evaluate it, and keep track of the best score.

This is not only tedious but also unreliable if you evaluate each combination on a single train-test split. As we've discussed, a single split can give misleading results due to the specific data points that happen to land in the test set.

To solve this, we combine hyperparameter searching with k-fold cross-validation. This ensures that each hyperparameter combination is evaluated robustly across multiple validation sets, giving us a more stable estimate of its performance.

The following video does an excellent job of demonstrating this manual, loop-based approach and showing why a more automated solution is necessary.

Machine Learning Tutorial Python - 16: Hyper parameter Tuning (GridSearchCV)

This video from codebasics provides a practical and intuitive walkthrough of the problem we're trying to solve.

First, watch the introduction explaining the problem. The presenter discusses the need to choose both a model and its hyperparameters and introduces k-fold cross-validation as a more robust evaluation method than a single train-test split. Next, watch the segment demonstrating the manual approach using nested for loops. This perfectly illustrates the inconvenience and inefficiency that GridSearchCV is designed to solve.


Grid Search: An Exhaustive Exploration

Grid Search is the most straightforward hyperparameter tuning method. You define a "grid" of discrete hyperparameter values you want to test. The algorithm then exhaustively trains and evaluates a model for every possible combination of these values, using cross-validation.

For example, if you specify:

  • C: [0.1, 1, 10] (3 values)
  • kernel: ['linear', 'rbf'] (2 values)

Grid search will test all 3 x 2 = 6 combinations. With 5-fold cross-validation, this means it will train and evaluate 6 * 5 = 30 models in total.

Scikit-Learn's GridSearchCV

Scikit-learn provides a convenient class, GridSearchCV, that automates this entire process. You provide it with:

  1. An estimator (e.g., SVC()).
  2. A param_grid dictionary mapping hyperparameter names to lists of values.
  3. A cv cross-validation strategy.
  4. A scoring metric to optimize (e.g., 'accuracy' or 'f1_score').

The codebasics video continues with a clear demonstration of how to use GridSearchCV.

Machine Learning Tutorial Python - 16: Hyper parameter Tuning (GridSearchCV)

This segment shows how GridSearchCV elegantly replaces the manual for loop approach.

Please watch the section introducing GridSearchCV. Pay close attention to how the parameter grid is defined and how the results, especially best_params_ and best_score_, are extracted after the search is complete. The use of a pandas DataFrame to view cv_results_ is also a very common and useful practice.

While GridSearchCV is simple and guarantees finding the best combination within your specified grid, its "curse of dimensionality" is apparent. As you add more hyperparameters or more values to test, the number of combinations grows exponentially, making it computationally infeasible for large search spaces.


Random Search: A More Efficient Strategy

This brings us to Random Search. Instead of exhaustively trying every combination, RandomizedSearchCV randomly samples a fixed number of combinations (n_iter) from the specified search space. For hyperparameters with continuous values (like C or alpha), you can even define a statistical distribution (e.g., a uniform or log-uniform distribution) from which to sample values.

But why would a random approach be better? Isn't it likely to miss the optimal combination?

The key insight is that not all hyperparameters are equally important. Some have a major impact on performance, while others have very little.

This image illustrates why Random Search can be more effective than Grid Search. Grid Search (left) tests many points, but only at a few distinct values for each parameter. If the optimal region (green peak) falls between these grid lines, it may miss it. Random Search (right) tests fewer points overall but explores a much wider and more diverse range of values for each parameter, increasing the probability of finding a good combination for the most important parameters.

Grid Search spends a lot of computational budget testing combinations where only an unimportant parameter is changed. Random Search, by sampling across the entire space, is more likely to hit upon a good value for the important parameters, often leading to better results in less time.

The Practical Implementation

The following reading assignment walks you through the scikit-learn API for both GridSearchCV and RandomizedSearchCV, providing full code examples for both classification and regression tasks.

Hyperparameter Optimization With Random Search and ...

This article provides a detailed, hands-on guide to implementing both search strategies in scikit-learn.

First, review the section on the Scikit-Learn API. This will familiarize you with the key arguments like cv, scoring, and n_jobs. Next, work through the section Hyperparameter Optimization for Classification. This provides a complete, runnable example using RandomizedSearchCV and GridSearchCV on a real dataset. Pay attention to how the search space is defined differently for each method (using a distribution for random search vs. a list for grid search). Finally, skim the section on Hyperparameter Optimization for Regression. The process is nearly identical, but the key difference is the choice of scoring metric (e.g., 'neg_mean_absolute_error'). This reinforces the general applicability of these techniques.


Grid Search vs. Random Search: Which to Choose?

You now have two powerful tools for hyperparameter tuning. Here's a quick summary to guide your choice:

  • Grid Search:

    • Pros: Exhaustive, guaranteed to find the best combination within the provided grid.
    • Cons: Computationally very expensive, suffers from the curse of dimensionality.
    • Best for: Small search spaces, or when you have a good idea of a narrow range of promising parameter values.
  • Random Search:

    • Pros: Much more efficient for large search spaces. Allows for sampling from continuous distributions. Often finds better models in less time.
    • Cons: Not guaranteed to find the absolute optimum; results have a random component.
    • Best for: Large search spaces, when you are less certain about the optimal parameter values and want to explore more widely.

A common workflow is to use Random Search first to explore a wide range of values and identify a promising region in the search space. Then, you can use Grid Search to perform a finer, more exhaustive search within that smaller, more promising region.

For a final perspective, this video offers a concise summary of the differences.

Hyperparameter Tuning For XGBoost Grid Search Vs Random Search Vs Bayesian Optimization Hyperopt

This video from Grab N Go Info provides a quick, high-level comparison of different tuning strategies.

Watch the introductory segment that compares the methods. It clearly summarizes the core difference: Grid Search is exhaustive and independent, while Random Search is a random sampling. It also introduces Bayesian Optimization, an even more advanced technique that uses past results to inform future searches—a great example of the "next evolution" you're interested in.


Conclusion

In this lesson, you've learned how to move beyond default model settings and systematically optimize your models for the best possible performance. This is a fundamental skill for any machine learning practitioner.

Key Takeaways:

  • Hyperparameter tuning is the process of finding the optimal configuration for a model's external settings.
  • Grid Search is an exhaustive, brute-force method that tests every combination in a predefined grid of values. It's thorough but computationally expensive.
  • Random Search is a more efficient method that samples a fixed number of combinations from a search space, which can include statistical distributions. It often finds better models faster, especially with large search spaces.
  • Cross-validation is an essential partner to hyperparameter tuning, providing a robust and reliable way to evaluate the performance of each tested combination.

What's Next:

You now know how to take a single machine learning model and tune it to its peak performance. But how do you decide which model to start with in the first place? Should you use an SVM, a Random Forest, or XGBoost? In our next lesson, we will develop a decision-making framework to select an appropriate machine learning algorithm based on problem type and data characteristics. This will prepare you for the final project, where you will apply all the skills you've learned to a new dataset from start to finish.