avatarFarhad Malik

Summary

The webpage provides an explanation of grid search, a method for optimizing hyperparameters in machine learning models, and demonstrates its implementation in Python using the scikit-learn library.

Abstract

The article on the webpage delves into the concept of grid search, an essential technique for determining the optimal hyperparameter values for machine learning models. It outlines the importance of hyperparameter tuning in the context of data science projects, emphasizing its role in enhancing model accuracy. The author breaks down the process into three key components of a data science project: data collection and feature engineering, model selection, and hyperparameter optimization. The article particularly focuses on the third component, discussing the challenges and resource demands of setting optimal hyperparameters. It introduces grid search as a solution to systematically explore and evaluate different combinations of hyperparameters, thereby streamlining the optimization process. The author also provides a practical guide to implementing grid search in Python, utilizing the scikit-learn library. This includes installing the library, importing necessary modules, setting up a dictionary of hyperparameters to test, and executing the grid search with a support vector classification (SVC) model as an example. The article concludes by illustrating how to retrieve and interpret the results to identify the best hyperparameter configuration.

Opinions

  • The author views hyperparameter tuning as a critical yet challenging aspect of machine learning projects, likening it to a "black art."
  • Grid search is portrayed as an effective and elegant method for hyperparameter optimization, capable of saving time and resources.
  • The article suggests that grid search is particularly useful when dealing with models that have a large number of hyperparameters, such as the SVC model in scikit-learn.
  • The author emphasizes the importance of choosing the right machine learning model and the significance of hyperparameters in controlling model accuracy.
  • The use of precision scoring as a performance metric indicates the author's preference for this measure when assessing model performance in the context of grid search.

What Is Grid Search?

Explaining How To Obtain Optimal Hyperparameter Values

This article aims to explain what grid search is and how we can use to obtain optimal values of model hyperparameters.

I will explain all of the required concepts in simple terms along with outlining how we can implement the grid search in Python.

Photo by Evgeni Tcherkasski on Unsplash

1. The Problem

For the sake of simplicity, we can divide the analytical aspects of a data science project into three parts:

  1. The first part would be all about gathering the required data and engineering the features.

2. The second part would revolve around choosing the right machine learning model.

3. The last part would be around finding the optimal hyperparameters.

Let’s understand the third part better because not only tuning hyperparameters is considered a black art, it is also a tedious task and takes time and effort.

This article aims to explain what grid search is and how we can use to obtain optimal values of model hyperparameters.

This is where the grid search can be extremely helpful because it can help us determine the optimal values in an elegant manner.

2. What Is A Hyperparameter?

A machine learning model has multiple parameters that are not trained by the training set. These parameters control the accuracy of the model. Therefore, the hyperparameters are particularly important in a data science project.

The hyperparameters are configured up-front and are provided by the caller of the model before the model is trained.

As an instance, the learning rate of a neural network is a hyperparameter because it is set by the caller before the training data is fed to the model. On the other hand, the weights of a neural network are not its hyperparameters because they are trained by the training dataset.

Furthermore, consider the Support Vector Classification (SVC) model which is used to classify data sets. There are a number of hyperparameters that the model requires.

Subsequently, the scikit-learn library version of SVC can be set up with a large number of hyperparameters, some of the common parameters are:

  1. C: This is a regularisation parameter
  2. Kernel: We can set the kernel parameter to linear, poly, rbf, sigmoid, precomputed or provide our own callable.
  3. Degree: We can pass in a custom degree to support the poly kernel parameter.
  4. Gamma: This is the coefficient for rbf, poly and sigmoid kernel parameter.
  5. Max_Iter: It is the maximum number of iterations for the solver.

Consider that we want to use the SVC model (for whatever reason). Setting the optimal values of the hyper-parameters can be challenging and resource-demanding. Imagine how many permutations we need to determine the best parameter values.

This is where Grid Search comes in.

3. What Is Grid Search?

Grid search is a tuning technique that attempts to compute the optimum values of hyperparameters. It is an exhaustive search that is performed on a the specific parameter values of a model. The model is also known as an estimator.

Grid search exercise can save us time, effort and resources.

4. Python Implementation

We can use the grid search in Python by performing the following steps:

1. Install sklearn library

pip install sklearn

2. Import sklearn library

from sklearn.model_selection import GridSearchCV

3. Import your model

from sklearn.svm import SVC

4. Create a list of hyperparameters dictionary

This is the key step.

Let’s consider that we want to find the optimal hyperparameter values for:

  • kernal: We want the model to train itself on the following kernels and give us the best value amongst linear, poly, rbf, sigmoid and precomputed values
  • C: we want the model to try the following values of C: [1,2,3,300,500]
  • max_iter: we want the model to use the following values of max_iter: [1000,100000] and give us the best value.

We can create the required dictionary:

parameters = [{
'kernel': ['linear', 'poly', 'rbf', 'sigmoid', 'precomputed'], 
'C': [1,2,3,300,500],
'max_iter': [1000,100000]}]

5. Instantiate GridSearchCV and pass in the parameters

clf = GridSearchCV(
        SVC(), parameters, scoring='accuracy'
    )
clf.fit(X_train, y_train)

Note: we decided to use the precision scoring measure to assess the performance.

6. Finally, print out the best parameters:

print(clf.best_params_)

That’s all.

We will now be presented with the optimal values of the hyperparameters.

The selected parameters are the ones that maximised the precision score.

5. Summary

This article explained how to use the Grid Search to obtain optimal hyperparameters for a machine learning model.

Fintechexplained
Machine Learning
Python
Data Science
Fintech
Recommended from ReadMedium