English translation
Load dataset
Grid search is suitable for fine-grained exploration over a small parameter space, whereas random search excels at exploring high-dimensional spaces. Both methods require clearly defined stopping criteria.
I record the runtime and performance score for each hyperparameter combination. The search process itself is an auditable experiment.
In the previous article, we discussed various hyperparameter tuning techniques—especially their critical role in the machine learning pipeline. Understanding how hyperparameters influence model performance is essential for improving model effectiveness. Now, we focus on two foundational hyperparameter optimization strategies: Grid Search and Random Search. These are widely adopted approaches, each with distinct advantages, limitations, and ideal use cases.
Grid Search
Grid search is a simple yet commonly used hyperparameter optimization technique. Its core idea is to define a discrete grid of candidate values for each hyperparameter and then exhaustively evaluate all combinations within that grid. By uniformly traversing the entire parameter space, grid search guarantees finding the globally optimal configuration (within the specified grid).
When comparing grid search and random search, first assess: parameter ranges, total number of combinations, training cost, validation score variance, and whether Bayesian optimization should be applied next.
Advantages
- Simple to implement; logic is transparent and intuitive.
- Guarantees finding the best combination within the predefined grid, especially effective when the parameter space is small.
Disadvantages
- Computationally expensive—time complexity grows exponentially with the number of hyperparameters and grid resolution.
- Lacks flexibility for sparse or irregularly distributed optima.
Example
Suppose we want to optimize the n_estimators (number of trees) and max_depth (maximum depth per tree) hyperparameters of a Random Forest classifier using grid search. We can implement this using GridSearchCV from scikit-learn.
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import load_iris
# Load dataset
data = load_iris()
X, y = data.data, data.target
# Define model
model = RandomForestClassifier()
# Define hyperparameter grid
param_grid = {
'n_estimators': [10, 50, 100],
'max_depth': [None, 10, 20]
}
# Initialize grid search
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=5)
# Perform search
grid_search.fit(X, y)
print("Best parameters:", grid_search.best_params_)
print("Best cross-validation score:", grid_search.best_score_)
In this example, we define a grid containing discrete values for n_estimators and max_depth. GridSearchCV automatically evaluates every combination using 5-fold cross-validation and returns the best-performing configuration.
Random Search
Random search is another widely used hyperparameter optimization method. Instead of enumerating all possible combinations, it randomly samples a fixed number of configurations from user-specified distributions over each hyperparameter. This approach is typically faster than grid search—and significantly more efficient when optimizing in high-dimensional or large-scale parameter spaces.
Before reading “Hyperparameter Optimization: Grid Search vs. Random Search”, use the accompanying diagram to confirm the main narrative flow. After reading, revisit the card to identify which steps you can execute immediately—and which require supplemental study.
Advantages
- More computationally efficient for large or high-dimensional parameter spaces.
- Can uncover non-intuitive, potentially superior configurations missed by structured grids.
Disadvantages
- Does not guarantee finding the global optimum—results depend on stochastic sampling.
- May require multiple runs to ensure robustness and reproducibility.
Example
Next, we apply RandomizedSearchCV to optimize the same Random Forest model.
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import randint
# Define model
model = RandomForestClassifier()
# Define hyperparameter distributions
param_distributions = {
'n_estimators': randint(10, 200), # Uniform integer distribution from 10 to 199
'max_depth': [None] + list(range(10, 30)) # None plus integers from 10 to 29
}
# Initialize random search
random_search = RandomizedSearchCV(
estimator=model,
param_distributions=param_distributions,
n_iter=20, # Number of random combinations to sample
cv=5
)
# Perform search
random_search.fit(X, y)
print("Best parameters:", random_search.best_params_)
print("Best cross-validation score:", random_search.best_score_)
Here, RandomizedSearchCV draws 20 random configurations from the specified distributions and evaluates each via 5-fold cross-validation. This strategy scales gracefully when hyperparameters have broad or continuous ranges—or when many hyperparameters are involved.
If you haven’t fully internalized “Hyperparameter Optimization: Grid Search vs. Random Search”, retrace the four actions outlined on this card.
When reviewing “Hyperparameter Optimization: Grid Search vs. Random Search”, avoid launching a full-scale project upfront. Instead, start with one simple working example to verify whether the core concepts are clear.
Summary
In this article, we examined two fundamental hyperparameter optimization techniques: Grid Search, which offers simplicity and completeness at the cost of computational efficiency; and Random Search, which trades theoretical completeness for practical scalability—especially valuable in high-dimensional settings. In the next article, we’ll explore Bayesian Optimization, a more advanced method that leverages past evaluation results to intelligently guide future searches—dramatically reducing the number of trials needed to locate high-performing configurations. Stay tuned!
Continue