Single logo
  • Courses
  • Articles
  • About
Join for freeJoin free
  1. Blog
  2. /
  3. S28L01 -Updated template with GridSearchCV

Machine Leaning Articles
1 views

S28L01 -Updated template with GridSearchCV


May 15, 2025

Mastering GridSearchCV for Optimal Machine Learning Models: A Comprehensive Guide

Table of Contents

  1. Introduction to GridSearchCV
  2. Understanding the Dataset
  3. Data Preprocessing
    • Handling Missing Data
    • Encoding Categorical Variables
    • Feature Selection
    • Feature Scaling
  4. Implementing GridSearchCV
    • Setting Up Cross-Validation with StratifiedKFold
    • GridSearchCV Parameters Explained
  5. Building and Tuning Machine Learning Models
    • K-Nearest Neighbors (KNN)
    • Logistic Regression
    • Gaussian Naive Bayes
    • Support Vector Machines (SVM)
    • Decision Trees
    • Random Forest
    • AdaBoost
    • XGBoost
  6. Performance Analysis
  7. Optimizing GridSearchCV
  8. Conclusion and Next Steps

1. Introduction to GridSearchCV

GridSearchCV is a technique in machine learning used for hyperparameter tuning. Hyperparameters are crucial parameters that govern the training process and the structure of the model. Unlike regular parameters, hyperparameters are set before the training phase begins and can significantly influence the model's performance.

GridSearchCV works by exhaustively searching through a specified parameter grid, evaluating each combination using cross-validation, and identifying the combination that yields the best performance based on a chosen metric (e.g., F1-score, accuracy).

Why GridSearchCV?

  • Comprehensive Search: Evaluates all possible combinations of hyperparameters.
  • Cross-Validation: Ensures that the model's performance is robust and not just tailored to a specific subset of data.
  • Automation: Streamlines the tuning process, saving time and computational resources.

However, it's essential to note that GridSearchCV can be computationally intensive, especially with large datasets and extensive parameter grids. This guide explores strategies to manage these challenges effectively.

2. Understanding the Dataset

For this demonstration, we utilize a dataset focused on airline passenger satisfaction. The dataset originally comprises over 100,000 records but has been pared down to 5,000 records for feasibility in this example. Each record encompasses 23 features, including demographic information, flight details, and satisfaction levels.

Sample of the Dataset

Gender Customer Type Age Type of Travel Class Flight Distance ... Satisfaction
Female Loyal Customer 41 Personal Travel Eco Plus 746 ... Neutral or Dissatisfied
Male Loyal Customer 53 Business Travel Business 3095 ... Satisfied
Male Disloyal Customer 21 Business Travel Eco 125 ... Satisfied
... ... ... ... ... ... ... ...

The target variable is Satisfaction, categorized as "Satisfied" or "Neutral or Dissatisfied."

3. Data Preprocessing

Effective data preprocessing is paramount to ensure that machine learning models perform optimally. The steps include handling missing data, encoding categorical variables, feature selection, and feature scaling.

Handling Missing Data

Numeric Data: Missing values in numerical columns are addressed using the mean imputation strategy.

from sklearn.impute import SimpleImputer
import numpy as np

# Initialize imputer for numeric data
imp_mean = SimpleImputer(missing_values=np.nan, strategy='mean')

# Identify numerical columns
numerical_cols = list(np.where((X.dtypes == np.int64) | (X.dtypes == np.float64))[0])

# Fit and transform the data
imp_mean.fit(X.iloc[:, numerical_cols])
X.iloc[:, numerical_cols] = imp_mean.transform(X.iloc[:, numerical_cols])

Categorical Data: For string-based columns, the most frequent value imputation strategy is employed.

# Initialize imputer for categorical data
imp_freq = SimpleImputer(missing_values=np.nan, strategy='most_frequent')

# Identify string columns
string_cols = list(np.where((X.dtypes == object))[0])

# Fit and transform the data
imp_freq.fit(X.iloc[:, string_cols])
X.iloc[:, string_cols] = imp_freq.transform(X.iloc[:, string_cols])

Encoding Categorical Variables

Categorical variables are transformed into a numerical format using Label Encoding and One-Hot Encoding.

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer

def LabelEncoderMethod(series):
    le = LabelEncoder()
    return le.fit_transform(series)

def OneHotEncoderMethod(indices, data):
    columnTransformer = ColumnTransformer(
        [('encoder', OneHotEncoder(), indices)],
        remainder='passthrough'
    )
    return columnTransformer.fit_transform(data)

def EncodingSelection(X, threshold=10):
    string_cols = list(np.where((X.dtypes == object))[0])
    one_hot_encoding_indices = []
    
    for col in string_cols:
        unique_vals = len(pd.unique(X[X.columns[col]]))
        if unique_vals == 2 or unique_vals > threshold:
            X[X.columns[col]] = LabelEncoderMethod(X[X.columns[col]])
        else:
            one_hot_encoding_indices.append(col)
                
    X = OneHotEncoderMethod(one_hot_encoding_indices, X)
    return X

# Apply encoding
X = EncodingSelection(X)

Feature Selection

To enhance model performance and reduce computational complexity, SelectKBest with the Chi-Squared (χ²) statistic is utilized to select the top 10 features.

from sklearn.feature_selection import SelectKBest, chi2
from sklearn.preprocessing import MinMaxScaler

# Initialize SelectKBest
kbest = SelectKBest(score_func=chi2, k=10)

# Scale features
scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)

# Fit SelectKBest
X_selected = kbest.fit_transform(X_scaled, y)

Feature Scaling

Feature scaling ensures that all features contribute equally to the model's performance.

from sklearn.preprocessing import StandardScaler

# Initialize StandardScaler
sc = StandardScaler(with_mean=False)

# Fit and transform the training data
sc.fit(X_train)
X_train = sc.transform(X_train)
X_test = sc.transform(X_test)

4. Implementing GridSearchCV

With the data preprocessed, the next step involves setting up GridSearchCV to tune hyperparameters for various machine learning models.

Setting Up Cross-Validation with StratifiedKFold

StratifiedKFold ensures that each fold of the cross-validation maintains the same proportion of class labels, which is crucial for imbalanced datasets.

from sklearn.model_selection import StratifiedKFold

# Initialize StratifiedKFold
cv = StratifiedKFold(n_splits=2)

GridSearchCV Parameters Explained

  • Estimator: The machine learning model to be tuned.
  • Param_grid: A dictionary defining the hyperparameters and their respective values to explore.
  • Verbose: Controls the verbosity; set to 1 to display progress.
  • Scoring: The performance metric to optimize, e.g., 'f1'.
  • n_jobs: Number of CPU cores to use; setting it to -1 utilizes all available cores.
from sklearn.model_selection import GridSearchCV

# Example: Setting up GridSearchCV for KNN
from sklearn.neighbors import KNeighborsClassifier

model = KNeighborsClassifier()
params = {
    'n_neighbors': [4, 5, 6, 7],
    'leaf_size': [1, 3, 5],
    'algorithm': ['auto', 'kd_tree'],
    'weights': ['uniform', 'distance']
}

grid_search_cv = GridSearchCV(
    estimator=model,
    param_grid=params,
    verbose=1,
    cv=cv,
    scoring='f1',
    n_jobs=-1
)

5. Building and Tuning Machine Learning Models

5.1 K-Nearest Neighbors (KNN)

KNN is a simple yet effective algorithm for classification tasks. GridSearchCV helps in selecting the optimal number of neighbors, leaf size, algorithm, and weighting scheme.

# Fit GridSearchCV
grid_search_cv.fit(X_train, y_train)

# Best parameters
print("Best Estimator", grid_search_cv.best_estimator_)
print("Best score", grid_search_cv.best_score_)

Output:

Best Estimator KNeighborsClassifier(leaf_size=1)
Best score 0.8774673417446253

5.2 Logistic Regression

Logistic Regression models the probability of a binary outcome. GridSearchCV tunes the solver type, penalty, and regularization strength.

from sklearn.linear_model import LogisticRegression

model = LogisticRegression()
params = {
    'solver': ['newton-cg', 'lbfgs', 'liblinear'],
    'penalty': ['l1', 'l2'],
    'C': [100, 10, 1.0, 0.1, 0.01]
}

grid_search_cv = GridSearchCV(
    estimator=model,
    param_grid=params,
    verbose=1,
    cv=cv,
    scoring='f1',
    n_jobs=-1
)

grid_search_cv.fit(X_train, y_train)
print("Best Estimator", grid_search_cv.best_estimator_)
print("Best score", grid_search_cv.best_score_)

Output:

Best Estimator LogisticRegression(C=0.01, solver='newton-cg')
Best score 0.8295203666687819

5.3 Gaussian Naive Bayes

Gaussian Naive Bayes assumes that the features follow a normal distribution. It has fewer hyperparameters, making it less intensive for GridSearchCV.

from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, classification_report

model_GNB = GaussianNB()
model_GNB.fit(X_train, y_train)
y_pred = model_GNB.predict(X_test)

print(accuracy_score(y_pred, y_test))
print(classification_report(y_pred, y_test))

Output:

0.84
              precision    recall  f1-score   support

           0       0.86      0.86      0.86       564
           1       0.82      0.81      0.82       436

    accuracy                           0.84      1000
   macro avg       0.84      0.84      0.84      1000
weighted avg       0.84      0.84      0.84      1000

5.4 Support Vector Machines (SVM)

SVMs are versatile classifiers that work well for both linear and non-linear data. GridSearchCV tunes the kernel type, regularization parameter C, degree, coefficient coef0, and kernel coefficient gamma.

from sklearn.svm import SVC

model = SVC()
params = {
    'kernel': ['linear', 'poly', 'rbf', 'sigmoid'],
    'C': [1, 5, 10],
    'degree': [3, 8],
    'coef0': [0.01, 10, 0.5],
    'gamma': ['auto', 'scale']
}

grid_search_cv = GridSearchCV(
    estimator=model,
    param_grid=params,
    verbose=1,
    cv=cv,
    scoring='f1',
    n_jobs=-1
)

grid_search_cv.fit(X_train, y_train)
print("Best Estimator", grid_search_cv.best_estimator_)
print("Best score", grid_search_cv.best_score_)

Output:

Best Estimator SVC(C=5, coef0=0.01)
Best score 0.9168629045108148

5.5 Decision Trees

Decision Trees partition the data based on feature values to make predictions. GridSearchCV optimizes parameters like the maximum number of leaf nodes and the minimum number of samples required to split an internal node.

from sklearn.tree import DecisionTreeClassifier

model = DecisionTreeClassifier()
params = {
    'max_leaf_nodes': list(range(2, 100)),
    'min_samples_split': [2, 3, 4]
}

grid_search_cv = GridSearchCV(
    estimator=model,
    param_grid=params,
    verbose=1,
    cv=cv,
    scoring='f1',
    n_jobs=-1
)

grid_search_cv.fit(X_train, y_train)
print("Best Estimator", grid_search_cv.best_estimator_)
print("Best score", grid_search_cv.best_score_)

Output:

Best Estimator DecisionTreeClassifier(max_leaf_nodes=29, min_samples_split=4)
Best score 0.9098148654372425

5.6 Random Forest

Random Forests aggregate multiple decision trees to improve performance and control overfitting. GridSearchCV tunes parameters like the number of estimators, maximum depth, number of features, and sample splits.

from sklearn.ensemble import RandomForestClassifier

model = RandomForestClassifier()
params = {
    'bootstrap': [True],
    'max_depth': [80, 90, 100, 110],
    'max_features': [2, 3],
    'min_samples_leaf': [3, 4, 5],
    'min_samples_split': [8, 10, 12],
    'n_estimators': [100, 200, 300, 1000]
}

grid_search_cv = GridSearchCV(
    estimator=model,
    param_grid=params,
    verbose=1,
    cv=cv,
    scoring='f1',
    n_jobs=-1
)

grid_search_cv.fit(X_train, y_train)
print("Best Estimator", grid_search_cv.best_estimator_)
print("Best score", grid_search_cv.best_score_)

Output:

Best Estimator RandomForestClassifier(max_leaf_nodes=82, min_samples_split=4)
Best score 0.9225835186933584

5.7 AdaBoost

AdaBoost combines multiple weak classifiers to form a strong classifier. GridSearchCV tunes the number of estimators and the learning rate.

from sklearn.ensemble import AdaBoostClassifier

model = AdaBoostClassifier()
params = {
    'n_estimators': np.arange(10, 300, 10),
    'learning_rate': [0.01, 0.05, 0.1, 1]
}

grid_search_cv = GridSearchCV(
    estimator=model,
    param_grid=params,
    verbose=1,
    cv=cv,
    scoring='f1',
    n_jobs=-1
)

grid_search_cv.fit(X_train, y_train)
print("Best Estimator", grid_search_cv.best_estimator_)
print("Best score", grid_search_cv.best_score_)

Output:

Best Estimator AdaBoostClassifier(learning_rate=1, n_estimators=30)
Best score 0.8938313525749858

5.8 XGBoost

XGBoost is a highly efficient and scalable implementation of gradient boosting. Due to its extensive hyperparameter space, GridSearchCV can be time-consuming.

import xgboost as xgb

model = xgb.XGBClassifier()
params = {
    'min_child_weight': [1, 5, 10],
    'gamma': [0.5, 1, 1.5, 2, 5],
    'subsample': [0.6, 0.8, 1.0],
    'colsample_bytree': [0.6, 0.8, 1.0],
    'max_depth': [3, 4, 5],
    'n_estimators': [100, 500, 1000],
    'learning_rate': [0.01, 0.3, 0.5, 0.1],
    'reg_lambda': [1, 2]
}

grid_search_cv = GridSearchCV(
    estimator=model,
    param_grid=params,
    verbose=1,
    cv=cv,
    scoring='f1',
    n_jobs=-1
)

grid_search_cv.fit(X_train, y_train)
print("Best Estimator", grid_search_cv.best_estimator_)
print("Best score", grid_search_cv.best_score_)

Output:

Best Estimator XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
              colsample_bynode=1, colsample_bytree=0.8, gamma=0.5, gpu_id=-1,
              importance_type='gain', interaction_constraints='',
              learning_rate=0.01, max_delta_step=0, max_depth=5,
              min_child_weight=1, missing=nan, monotone_constraints='()',
              n_estimators=500, n_jobs=12, num_parallel_tree=1, random_state=0,
              reg_alpha=0, reg_lambda=1, scale_pos_weight=1, subsample=1.0,
              tree_method='exact', validate_parameters=1, verbosity=None)
Best score 0.9267223852716081

Note: The XGBoost GridSearchCV run is notably time-consuming due to the vast number of hyperparameter combinations.

6. Performance Analysis

After tuning, each model presents varying levels of performance based on the best F1-scores achieved:

  • KNN: 0.877
  • Logistic Regression: 0.830
  • Gaussian Naive Bayes: 0.840
  • SVM: 0.917
  • Decision Tree: 0.910
  • Random Forest: 0.923
  • AdaBoost: 0.894
  • XGBoost: 0.927

Interpretation

  • XGBoost and Random Forest exhibit the highest F1-scores, indicating superior performance on the dataset.
  • SVM also demonstrates robust performance.
  • KNN and AdaBoost provide competitive results with slightly lower F1-scores.
  • Logistic Regression and Gaussian Naive Bayes, while simpler, still offer respectable performance metrics.

7. Optimizing GridSearchCV

Given the computational intensity of GridSearchCV, especially with large datasets or extensive parameter grids, it's crucial to explore optimization strategies:

7.1 RandomizedSearchCV

Unlike GridSearchCV, RandomizedSearchCV samples a fixed number of parameter settings from specified distributions. This approach can significantly reduce computation time while still exploring a diverse set of hyperparameters.

from sklearn.model_selection import RandomizedSearchCV

# Example setup for RandomizedSearchCV
random_search_cv = RandomizedSearchCV(
    estimator=model,
    param_distributions=params,
    n_iter=100,  # Number of parameter settings sampled
    verbose=1,
    cv=cv,
    scoring='f1',
    n_jobs=-1,
    random_state=42
)

random_search_cv.fit(X_train, y_train)
print("Best Estimator", random_search_cv.best_estimator_)
print("Best score", random_search_cv.best_score_)

7.2 Reducing Parameter Grid Size

Focus on hyperparameters that significantly impact model performance. Conduct exploratory analyses or leverage domain knowledge to prioritize certain parameters over others.

7.3 Utilizing Parallel Processing

Setting n_jobs=-1 in GridSearchCV allows the use of all available CPU cores, accelerating the computation process.

7.4 Early Stopping

Implement early stopping mechanisms to halt the search once a satisfactory performance level is achieved, preventing unnecessary computations.

8. Conclusion and Next Steps

GridSearchCV is an indispensable tool for hyperparameter tuning, offering a systematic approach to enhance machine learning model performance. Through meticulous data preprocessing, strategic parameter grid formulation, and leveraging computational optimizations, data scientists can harness GridSearchCV's full potential.

Next Steps:

  • Explore RandomizedSearchCV for more efficient hyperparameter tuning.
  • Implement Cross-Validation Best Practices to ensure model robustness.
  • Integrate Feature Engineering Techniques to further improve model performance.
  • Deploy Optimized Models in real-world scenarios, monitoring their performance over time.

By mastering GridSearchCV and its optimizations, you're well-equipped to build high-performing, reliable machine learning models that stand the test of varying data landscapes.

Share Article

Back to Blog

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.