Model Optimization in Machine Learning: Random vs. Grid?Search
Ricardo Neves Junior, PhD
AI Engineer | Senior Data Scientist | LLM | RAG | GenAI | Machine Learning | NLP | Azure | AWS
What is a Machine Learning?model?
A machine learning model is a mathematical formula with several parameters that need to be learned from data. This process is known as training. In other words, training with data
Machine Learning algorithms rely on another type of parameter, called Hyperparameter. Hyperparameters are defined before the training process, therefore, they cannot be learned.
Now let’s reflect on some examples of Hyperparameters of the classification algorithm called Random Forest:
The problem:
Given that hyperparameter values must be specified before training, there are several situations in which it is not possible to adequately determine the best values for each hyperparameter.
Search Space
The image bellow describing the search space of an optimization algorithm with local minima and maxima, as well as global minima and maxima, would be a visual representation to understand how these algorithms navigate a set of possible solutions to find the best (optimal) result.?.
Faced with this problem, several researchers have presented intelligent optimization algorithms for optimizing hyperparameters
Grid Search
Grid Search is a technique used to find the best combination of parameters for a given model in machine learning. This technique involves defining a “grid” of parameters, which are all the possible combinations of different parameter values that you want to test for your model.
Advantages of Grid Search
Disadvantages of Grid Search
Random Search
The Random Search algorithm, is a hyperparameter optimization method used in machine learning, which randomly selects combinations of parameters from a distribution specified for each hyperparameter. Unlike Grid Search, which tests all possible combinations of a predefined grid of parameter values, Random Search explores the hyperparameter space randomly. This can be more efficient, especially in high-dimensional spaces, as it allows for a more comprehensive search that is less restricted to a specific grid, and often finds a good solution with far fewer iterations and computation time compared to Grid Search.
Advantages of Random?Search
领英推荐
Disadvantages of Random?Search
Grid Search x Random Search
Let’s to Benchmark Results
First, we need to import tle libraries:
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn.svm import SVC
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import numpy as np
import time
For this benchmarking experiment, we will utilize the Iris Dataset
# Load data
iris = load_iris()
X, y = iris.data, iris.target
# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
Model Definition
model = SVC()
Grid Search Application
# Parameters for Grid Search
param_grid = {'C': [0.1, 1, 10, 100], 'gamma': [1, 0.1, 0.01, 0.001], 'kernel': ['rbf', 'poly', 'sigmoid']}
# Start the timer for Grid Search
start_time = time.time()
# Execute Grid Search
grid_search = GridSearchCV(model, param_grid, refit=True, verbose=0)
grid_search.fit(X_train, y_train)
# Calculate the execution time of Grid Search
grid_search_time = time.time() - start_time
print("Best score of Grid Search: ", grid_search.best_score_)
print("Processing time of Grid Search: {:.2f} seconds".format(grid_search_time))
Best score of Grid Search: 0.971
Processing time of Grid Search: 0.96 seconds
Random Search Application
# Parameters for Random Search
param_distributions = {'C': np.logspace(-2, 2, 100), 'gamma': np.logspace(-3, 1, 100), 'kernel': ['rbf', 'poly', 'sigmoid']}
# Start the timer for Random Search
start_time = time.time()
# Execute Random Search
random_search = RandomizedSearchCV(model, param_distributions, n_iter=50, refit=True, verbose=0, random_state=42)
random_search.fit(X_train, y_train)
# Calculate the execution time of Random Search
random_search_time = time.time() - start_time
print("Best score of Random Search: ", random_search.best_score_)
print("Processing time of Random Search: {:.2f} seconds".format(random_search_time))
Best score of Random Search: 0.96
Processing time of Random Search: 0.55 seconds
Results and Discussion
The benchmarking experiment results
Firstly, the Grid Search algorithm achieved a slightly higher score of 0.97 compared to the Random Search’s 0.96. This difference, in percentage terms, is approximately 1.19%. While this indicates a marginally better performance by the Grid Search in accuracy, the difference is relatively small.
However, when considering processing time, the distinction becomes more significant. The Grid Search took 0.96 seconds, whereas the Random Search completed in just 0.55 seconds. This means that Random Search was approximately 42.7% faster than Grid Search. In scenarios where processing speed is crucial, such as in real-time applications, this difference in time efficiency can be highly impactful.
While both algorithms performed well, Random Search stands out as the more efficient option in this experiment. Its ability to achieve nearly similar accuracy to Grid Search but in significantly less processing time underlines its suitability for situations where quick decision-making is essential. This advantage can be particularly valuable in real-world applications where computational resources and time are critical factors.