How to tune Hyper parameters using Grid Search in Python?

How to tune Hyper parameters using Grid Search in Python?

How to tune Hyper parameters using Grid Search in Python?

This recipe helps you tune Hyper parameters using Grid Search in Python

This data science python source code does the following: 1. Different methods for Hyperparameter tuning a model. 2. Implements GridSearhCV using Cross Validation method. 3. Setting up parameters for GridSearchCV. 4. Obtains the best parameters and best result.
In [1]:
## How to tune Hyper-parameters using Grid Search in Python
def Snippet_142():
    print(format('How to tune Hyper-parameters using Grid Search in Python','*^82'))

    import warnings

    # load libraries
    import numpy as np
    from sklearn import linear_model, datasets
    from sklearn.model_selection import GridSearchCV

    # Load data
    iris = datasets.load_iris()
    X =
    y =

    # Create logistic regression
    logistic = linear_model.LogisticRegression()

    # Create Hyperparameter Search Space
    # Create regularization penalty space
    penalty = ['l1', 'l2']

    # Create regularization hyperparameter space
    C = np.logspace(0, 4, 10)

    # Create hyperparameter options
    hyperparameters = dict(C=C, penalty=penalty)

    # Create grid search using 5-fold cross validation
    clf = GridSearchCV(logistic, hyperparameters, cv=5, verbose=0)

    # Fit grid search
    best_model =, y)

    # View best hyperparameters
    print('Best Penalty:', best_model.best_estimator_.get_params()['penalty'])
    print('Best C:', best_model.best_estimator_.get_params()['C'])

*************How to tune Hyper-parameters using Grid Search in Python*************
Best Penalty: l1
Best C: 7.742636826811269

Relevant Projects

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.