Mastering Hyperparameter Tuning: Unlocking the Full Potential of Machine Learning Models

Brijesh Soni
7 min readMar 17, 2023

--

Image by Google

Introduction

Machine learning models are developed by training them on a dataset using a set of hyperparameters that define the model’s structure and complexity. Hyperparameters are the parameters that are defined before the training process begins, unlike the model parameters that are learned during the training process. Hyperparameter tuning is the process of finding the optimal set of hyperparameters for a machine-learning model that results in the best performance on the validation dataset.

Hyperparameter Tuning ????

Image by Google

Hyperparameter tuning is one of the most important aspects of developing a successful machine-learning model. The choice of hyperparameters affects the model’s ability to learn the patterns in the data, the model’s computational efficiency, and the model’s generalization ability. Therefore, finding the optimal set of hyperparameters is crucial for developing a model that performs well on unseen data.

Some important points about Hyperparameter tuning

  1. Hyperparameter tuning is the process of selecting the optimal set of hyperparameters for a machine-learning model.
  2. Manual tuning requires a deep understanding of the model’s behavior and is time-consuming.
  3. Grid search is systematic and searches the hyperparameter space exhaustively, but it can be computationally expensive for large hyperparameter spaces.
  4. Random search is a more efficient approach that samples hyperparameters randomly from the hyperparameter space.
  5. Bayesian optimization uses probabilistic models to guide the search in the hyperparameter space.
  6. Genetic algorithms are inspired by natural selection and use a population of candidate solutions to find the optimal set of hyperparameters.
  7. The selection of a hyperparameter tuning technique depends on various factors such as the size of the hyperparameter space, the resources available, and the expertise of the practitioner.

Practical Implementation

Image by Brijesh Soni

Hyperparameter tuning can be implemented in Python using various libraries such as sci-kit-learn, Keras, and TensorFlow.

Here, we will focus on using the “sci-kit-learn’s GridSearchCV” function for hyperparameter tuning.

Step 1: Import the necessary libraries

import numpy as np
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV

Step 2: Load the dataset

We will use the iris dataset from sci-kit-learn.

iris = datasets.load_iris()
X = iris.data
y = iris.target

Step 3: Split the dataset into training and validation sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Step 4: Define the hyperparameter space

We will define a range of hyperparameters for the RandomForestClassifier.

param_grid = {
'n_estimators': [100, 200, 300],
'max_depth': [5, 10, 15, 20],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4]
}

Step 5: Create the model

rfc = RandomForestClassifier()

Step 6: Create the GridSearchCV object

grid_search = GridSearchCV(estimator=rfc, param_grid=param_grid, cv=5, n_jobs=-1)

Step 7: Fit the GridSearchCV object to the data

grid_search.fit(X_train, y_train)

Step 8: Print the best hyperparameters

print(grid_search.best_params_)

Step 9: Evaluate the model with the best hyperparameters

best_rfc = RandomForestClassifier(**grid_search.best_params_)
best_rfc.fit(X_train, y_train)
score = best_rfc.score(X_test, y_test)
print("Accuracy: %.2f%%" % (score*100))

Another example for my favorite Boston dataset👇

import numpy as np
from sklearn.datasets import load_boston
from sklearn.linear_model import Ridge
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import mean_squared_error

# Load Boston dataset
boston = load_boston()

# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(boston.data, boston.target, test_size=0.3, random_state=42)

# Set up the hyperparameter grid
param_grid = {'alpha': np.logspace(-4, 4, 9)}

# Create a Ridge regression model
model = Ridge()

# Perform grid search cross-validation
grid_search = GridSearchCV(model, param_grid, cv=5)
grid_search.fit(X_train, y_train)

# Print the best hyperparameters and the corresponding score
print("Best hyperparameters:", grid_search.best_params_)
print("Best score:", grid_search.best_score_)

# Use the best model to make predictions on the test set
best_model = grid_search.best_estimator_
y_pred = best_model.predict(X_test)

# Calculate the root mean squared error of the predictions
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
print("RMSE:", rmse)

In this code, we first load the Boston dataset using the sci-kit-learn library. We then split the dataset into training and testing sets using the train_test_split function.

Next, we set up a hyperparameter grid using a dictionary that contains a range of values for the alpha hyperparameter of the Ridge regression model. We create a Ridge regression model and use the GridSearchCV function to perform a grid search cross-validation using the training data and the hyperparameter grid.

After finding the best hyperparameters, we print the best hyperparameters and the corresponding score. We then use the best model to make predictions on the test set and calculate the root mean squared error of the predictions.

The best technique for Hyperparameter tuning

Image by Brijesh Soni

There is no one “best” technique for hyperparameter tuning as it ultimately depends on the problem domain and the specific machine learning algorithm being used.

However, some popular techniques for hyperparameter tuning are:

  1. Grid search: Grid search is a systematic approach that searches the hyperparameter space exhaustively. It can be computationally expensive for large hyperparameter spaces but is guaranteed to find the optimal solution within the search space.
  2. Random Search: Random search is a more efficient approach that samples hyperparameters randomly from the hyperparameter space. It is less computationally expensive than grid search and has been shown to be effective in finding good hyperparameters.
  3. Bayesian optimization: Bayesian optimization uses probabilistic models to guide the search in the hyperparameter space. It can be very efficient in finding good hyperparameters and requires fewer function evaluations than grid search and random search.
  4. Genetic algorithms: Genetic algorithms are inspired by the process of natural selection and use a population of candidate solutions to find the optimal set of hyperparameters. It is a more complex approach than grid search, random search, and Bayesian optimization, but it can be effective for non-convex and non-smooth search spaces.

It is recommended to try multiple techniques for hyperparameter tuning and compare their results to choose the best one for your specific problem. Additionally, it is important to have a good understanding of the machine learning algorithm being used and the hyperparameters that can be tuned for optimal performance.

Key differences between GridSearchCV and RandomizedSearchCV👇👇

Image by Brijesh Soni

Hyperparameter Space and Data Leakage: what are they?

Image by Brijesh Soni

Hyperparameter Space

In machine learning, hyperparameters are model parameters that are not learned from the training data, but rather set prior to training and remain fixed throughout the training process. These hyperparameters can have a significant impact on the performance of the model, and finding the best values for them can be a challenging task.

The hyperparameter space refers to the range of possible values for each hyperparameter. This space can be discrete or continuous and can range from a few possible values to an infinite number of values. The goal of hyperparameter tuning is to search this space to find the set of hyperparameters that leads to the best performance on the test data.

Data Leakage

Data leakage is a common problem in machine learning where information from the test set is leaked into the training set, leading to overly optimistic performance estimates. This can happen when the model is trained on data that includes information from the test set, such as when the hyperparameters are tuned using the test set.

For example, if the hyperparameters are tuned using the test set, the model may learn to perform well on the test set but not generalize well to new data. This is because the test set was used to optimize the hyperparameters, so the model was effectively trained on the test set.

To avoid data leakage, it is important to split the data into training, validation, and test sets, and use the validation set to tune the hyperparameters. This ensures that the model is not being optimized directly on the test set and can better generalize to new data. Additionally, techniques such as cross-validation can be used to further reduce the risk of data leakage.

Conclusion

Hyperparameter tuning is a crucial step in developing a successful machine-learning model. The choice of hyperparameters affects the model’s performance, computational efficiency, and generalization ability.

There are several techniques for hyperparameter tuning, including manual tuning, grid search, random search, Bayesian optimization, and genetic algorithms.

In this article, we focused on using sci-kit-learn’s GridSearchCV function for hyperparameter tuning. By following the implementation steps, we can easily perform hyperparameter tuning on our machine-learning models and find the optimal set of hyperparameters for better performance.

If you like my notes, then you should support me to make more such notes.

👋👋Stay tuned and Happy learning!!👋👋

Find me here👇

GitHub || Linkedin || Profile Summary

--

--

Brijesh Soni

🤖 Deep Learning Researcher 🤖 and Join as Data Science volunteer on @ds_chat_bot 👉👉 https://www.instagram.com/ds_chat_bot/