← Back to list

Day 33: Hyperparameter Tuning with Grid Search and Random Search

Imagine you’re a chef perfecting your signature dish — you tweak the spices, adjust the cooking time, and experiment with different…

Ian Clemence · 2025-04-06 11:36 · 6 claps · 3.2 min read
#data-science #machine-learning #hyperparameter-tuning #gridsearchcv #randomsearchcv
Open on Medium ↗
Wiki topics: ML · Machine Learning EDU · Education & Learning 🔬 · Science · General 🍳 · Food & Cooking 🏔️ · Outdoor & Adventure

Day 33: Hyperparameter Tuning with Grid Search and Random Search

Imagine you’re a chef perfecting your signature dish — you tweak the spices, adjust the cooking time, and experiment with different techniques until you get that flawless flavor. In the world of machine learning, hyperparameter tuning works in a very similar way. Today, we’re diving into how to fine-tune your models using Grid Search and Random Search to optimize performance. Welcome to Day 33 of “100 Days of Data Science,” where we transform good models into great ones by finding that perfect balance in your algorithm’s settings.

What Are Hyperparameters?

Hyperparameters are the configuration settings for your machine learning algorithms that you must define before training. For instance, in a Random Forest classifier, the number of trees (n_estimators) and the maximum depth of each tree (max_depth) are hyperparameters. The right settings can dramatically improve model performance, much like the right combination of ingredients enhances a recipe.

Grid Search vs. Random Search

  • Grid Search: Grid Search exhaustively evaluates every combination of hyperparameters from a predefined list. This method is thorough but can be time-consuming if the hyperparameter space is large.
  • Random Search: Random Search, by contrast, samples random combinations from the hyperparameter space. This method is often more efficient and can quickly lead you to a good set of parameters when you’re not sure which ones are most important.

A Real-World Scenario: Optimizing a Random Forest for Customer Churn Prediction

Imagine you work for a telecom company and want to predict customer churn. Your model’s accuracy depends on hyperparameters like the number of trees, the depth of each tree, and the minimum number of samples required at a leaf node. Tuning these settings with Grid Search or Random Search can help you build a model that reliably forecasts which customers are likely to churn.

Implementing Hyperparameter Tuning in Python

Step 1: Import Libraries and Load Data

We’ll use the Iris dataset to demonstrate hyperparameter tuning with a Random Forest classifier.

import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split, GridSearchCV, RandomizedSearchCV
from sklearn.metrics import classification_report
import matplotlib.pyplot as plt
import seaborn as sns

# Load the Iris dataset
iris = load_iris()
X = pd.DataFrame(iris.data, columns=iris.feature_names)
y = iris.target

# Split the data (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Step 2: Grid Search Example

Define a parameter grid and use Grid Search to explore different combinations.

# Define parameter grid
param_grid = {
    'n_estimators': [50, 100, 150],
    'max_depth': [None, 4, 6, 8],
    'min_samples_split': [2, 5, 10]
}

# Initialize Grid Search with cross-validation
grid_search = GridSearchCV(estimator=RandomForestClassifier(random_state=42),
                           param_grid=param_grid,
                           cv=5, 
                           scoring='accuracy',
                           n_jobs=-1)

# Fit Grid Search on the training data
grid_search.fit(X_train, y_train)

# Best parameters and score
print("Best Grid Search Parameters:", grid_search.best_params_)
print("Best Grid Search Accuracy:", grid_search.best_score_)

# Evaluate on the test set
best_model_grid = grid_search.best_estimator_
y_pred_grid = best_model_grid.predict(X_test)
print("\nClassification Report (Grid Search):")
print(classification_report(y_test, y_pred_grid, target_names=iris.target_names))

Step 3: Random Search Example

For a broader search, use Random Search to sample parameter combinations.

from scipy.stats import randint

# Define parameter distributions for Random Search
param_dist = {
    'n_estimators': randint(50, 200),
    'max_depth': [None, 4, 6, 8, 10],
    'min_samples_split': randint(2, 11)
}

# Initialize Random Search with cross-validation
random_search = RandomizedSearchCV(estimator=RandomForestClassifier(random_state=42),
                                   param_distributions=param_dist,
                                   n_iter=20,
                                   cv=5,
                                   scoring='accuracy',
                                   n_jobs=-1,
                                   random_state=42)

# Fit Random Search on the training data
random_search.fit(X_train, y_train)

# Best parameters and score from Random Search
print("Best Random Search Parameters:", random_search.best_params_)
print("Best Random Search Accuracy:", random_search.best_score_)

# Evaluate the best model on the test set
best_model_random = random_search.best_estimator_
y_pred_random = best_model_random.predict(X_test)
print("\nClassification Report (Random Search):")
print(classification_report(y_test, y_pred_random, target_names=iris.target_names))

Step 4: Visualizing Cross-Validation Results

Visualizing the performance across different hyperparameter settings can offer insights into model stability.

# Convert Grid Search results to a DataFrame
cv_results = pd.DataFrame(grid_search.cv_results_)
plt.figure(figsize=(10, 6))
sns.boxplot(x='param_max_depth', y='mean_test_score', data=cv_results)
plt.title('Grid Search CV Scores by Max Depth')
plt.xlabel('Max Depth')
plt.ylabel('Mean Accuracy Score')
plt.show()

Additional Resources

Final Thoughts

Hyperparameter tuning is the key to unlocking the full potential of your machine learning models. Whether you choose the exhaustive approach of Grid Search or the efficiency of Random Search, fine-tuning your model’s settings can lead to significant performance gains. Just like a chef perfects their recipe with careful adjustments, you can optimize your model to make more accurate predictions. I hope this guide has provided you with a clear, practical understanding of these tuning methods.

Thank you for joining me on Day 33. Stay tuned for Day 34, where we’ll explore yet another exciting topic in our data science journey. Until then, keep experimenting, stay curious, and happy coding!


메타데이터
post_id
9d878cdbfaf2
slug
day-33-hyperparameter-tuning-with-grid-search-and-random-search-9d878cdbfaf2
url
https://medium.com/@ianclemence/day-33-hyperparameter-tuning-with-grid-search-and-random-search-9d878cdbfaf2
canonical_url
https://medium.com/@ianclemence/day-33-hyperparameter-tuning-with-grid-search-and-random-search-9d878cdbfaf2
author_url
https://medium.com/@ianclemence
status
ok
fetched_at
2026-08-30 12:20:29