How to tune Hyper parameters using Random Search in Python?

How to tune Hyper parameters using Random Search in Python?

How to tune Hyper parameters using Random Search in Python?

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

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

    import warnings

    # load libraries
    from scipy.stats import uniform
    from sklearn import linear_model, datasets
    from sklearn.model_selection import RandomizedSearchCV

    # 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 distribution using uniform distribution
    C = uniform(loc=0, scale=4)

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

    # Create randomized search 5-fold cross validation and 100 iterations
    clf = RandomizedSearchCV(logistic, hyperparameters, random_state=1, n_iter=100,
                             cv=5, verbose=0, n_jobs=-1)

    # Fit randomized 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 Random Search in Python************
Best Penalty: l1
Best C: 1.668088018810296

Relevant Projects

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

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.

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.

Data Science Project-All State Insurance Claims Severity Prediction
Data science project in R to develop automated methods for predicting the cost and severity of insurance claims.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

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.

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.