π Blog Title: Mastering Hyperparameter Tuning in Machine Learning

Table of contents
- π§ Introduction
- β Why Hyperparameter Tuning is Important
- βοΈ Common Hyperparameters in ML Models
- π Hyperparameter Tuning Techniques
- π οΈ Sklearn Example with GridSearchCV & RandomizedSearchCV
- π§ Hyperparameter Tuning in XGBoost & LightGBM
- π‘ Best Practices for Tuning
- π Comparison Table: Techniques vs Time vs Accuracy
- β Pros
- β οΈ Cons
- π§© Final Thoughts
- π¬ Subscribe
π§ Introduction
In machine learning, achieving high model accuracy isn't just about choosing the right algorithmβtuning hyperparameters can make a huge difference. Hyperparameters are settings that govern the learning process and directly affect model performance.
β Why Hyperparameter Tuning is Important
Hyperparameter tuning helps:
Boost accuracy
Avoid underfitting/overfitting
Improve generalization
Speed up convergence
Incorrect hyperparameters can ruin even the best data or algorithms.
βοΈ Common Hyperparameters in ML Models
Algorithm | Common Hyperparameters |
Random Forest | n_estimators , max_depth , min_samples_split |
SVM | C , kernel , gamma |
XGBoost | learning_rate , n_estimators , max_depth , subsample |
KNN | n_neighbors , weights , metric |
Neural Nets | learning_rate , batch_size , epochs , activation |
π Hyperparameter Tuning Techniques
There are several strategies to tune hyperparameters. Each has its trade-offs in terms of accuracy, time, and complexity.
1οΈβ£ Grid Search
π What is it?
Tries all possible combinations of specified hyperparameter values.
π¦ Example:
If n_estimators = [50, 100] and max_depth = [3, 5], Grid Search tries:
(50, 3)
(50, 5)
(100, 3)
(100, 5)
β Pros:
- Exhaustive, can find the optimal combo
β Cons:
Computationally expensive
Not scalable to large hyperparameter spaces
2οΈβ£ Random Search
π What is it?
Samples random combinations from a defined space.
π¦ Example:
From the same grid above, it might try only 2 or 3 random combinations instead of all 4.
β Pros:
More efficient than grid search
Often finds good results faster
β Cons:
- May miss optimal settings
3οΈβ£ Bayesian Optimization
π What is it?
Uses probabilistic models (like Gaussian Processes) to model the function and decide where to search next.
β Pros:
Smarter and faster than random/grid search
Fewer evaluations needed
β Cons:
Complex to implement
Slower per iteration due to model buildin
4οΈβ£ Genetic Algorithms (GA)
π What is it?
Inspired by evolution. Uses selection, crossover, and mutation to evolve better hyperparameter combinations.
β Pros:
Handles complex search spaces
Works well with large models
β Cons:
Requires tuning GA parameters
Computationally heavy
5οΈβ£ Hyperband
π What is it?
A resource-aware algorithm that allocates more resources to better-performing configurations and eliminates poor ones early.
β Pros:
Extremely efficient
Good for deep learning models
β Cons:
May need early-stopping criteria
Implementation more complex than Grid/Random Search
π οΈ Sklearn Example with GridSearchCV & RandomizedSearchCV
Letβs walk through practical examples using Scikit-learn. Weβll tune hyperparameters for a Random Forest Classifier.
β Step-by-Step: GridSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
# Load data
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Define the model
model = RandomForestClassifier()
# Define hyperparameter grid
param_grid = {
'n_estimators': [50, 100],
'max_depth': [3, 5, None],
'min_samples_split': [2, 5]
}
# Setup GridSearchCV
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=5, scoring='accuracy')
# Fit
grid_search.fit(X_train, y_train)
# Best Parameters & Accuracy
print("Best Parameters:", grid_search.best_params_)
y_pred = grid_search.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
π² Step-by-Step: RandomizedSearchCV
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import randint
# Define parameter distribution
param_dist = {
'n_estimators': randint(50, 200),
'max_depth': [3, None],
'min_samples_split': randint(2, 10)
}
# Setup RandomizedSearchCV
random_search = RandomizedSearchCV(estimator=model, param_distributions=param_dist,
n_iter=10, cv=5, scoring='accuracy', random_state=42)
# Fit
random_search.fit(X_train, y_train)
# Best Parameters & Accuracy
print("Best Parameters:", random_search.best_params_)
y_pred = random_search.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
π§ Hyperparameter Tuning in XGBoost & LightGBM
πΌ XGBoost Hyperparameter Tuning Example
import xgboost as xgb
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
# Load data
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Define the model
model = xgb.XGBClassifier(use_label_encoder=False, eval_metric='logloss')
# Define hyperparameter grid
param_grid = {
'n_estimators': [100, 200],
'max_depth': [3, 5, 7],
'learning_rate': [0.01, 0.1, 0.2],
'subsample': [0.8, 1.0]
}
# GridSearchCV
grid = GridSearchCV(model, param_grid, cv=3, scoring='accuracy')
grid.fit(X_train, y_train)
print("Best Params:", grid.best_params_)
print("Best Score:", grid.best_score_)
π LightGBM Hyperparameter Tuning Example
import lightgbm as lgb
from sklearn.model_selection import RandomizedSearchCV
# Define the model
model = lgb.LGBMClassifier()
# Define hyperparameter space
param_dist = {
'num_leaves': [31, 50, 100],
'learning_rate': [0.01, 0.05, 0.1],
'n_estimators': [100, 200, 500],
'max_depth': [-1, 10, 20],
'min_child_samples': [10, 20, 30]
}
# RandomizedSearchCV
random_search = RandomizedSearchCV(model, param_distributions=param_dist,
n_iter=10, cv=3, scoring='accuracy', random_state=42)
random_search.fit(X_train, y_train)
print("Best Params:", random_search.best_params_)
print("Best Score:", random_search.best_score_)
π‘ Best Practices for Tuning
Always start with a baseline model to compare improvements.
Use RandomizedSearchCV for large search spaces; it's faster.
Try cross-validation (cv=5 or cv=10) for stable results.
Use domain knowledge to narrow down ranges.
Tune in stages: e.g., first n_estimators, then max_depth, then learning_rate.
π Comparison Table: Techniques vs Time vs Accuracy
Technique | Time Efficiency | Accuracy Boost | Best For |
Grid Search | β Slow | β High | Small, specific ranges |
Random Search | β Faster | β Good | Large search space |
Bayesian Optimization | β β Very Fast | β β Excellent | Complex models |
Genetic Algorithm | β Medium | β High | Exploration of complex spaces |
Hyperband | β β Very Fast | β Good | Large models, early stopping |
β Pros
Boosts model performance.
Helps discover optimal configurations.
Works for any ML algorithm.
β οΈ Cons
Can be computationally expensive.
Risk of overfitting if not validated properly.
Some techniques (e.g., Bayesian) require advanced libraries.
π§© Final Thoughts
Hyperparameter tuning is essential to extract the best performance from your models. While it's easy to overlook, it can often be the difference between a mediocre and a top-performing model. Start simple, validate thoroughly, and scale tuning as your models grow more complex.
π¬ Subscribe
If you found this guide helpful, follow for more content on Machine Learning, Deep Learning, and Model Optimization!
Thanks for Reading π.
Subscribe to my newsletter
Read articles from Tilak Savani directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
