How to tune Hyper parameters using Random Search in Python?
HYPERPARAMETER TUNING DATA CLEANING PYTHON DATA MUNGING MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

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

0

Recipe Objective

Many a times while working on a dataset and using a Machine Learning model we don"t know which set of hyperparameters will give us the best result. Passing all sets of hyperparameters manually through the model and checking the result might be a hectic work and may not be possible to do.

To get the best set of hyperparameters we can use Grid Search. Random Search passes Random combinations of hyperparameters one by one into the model and check the result. Finally it gives us the set of hyperparemeters which gives the best result after passing in the model.

So this recipe is a short example of how can tune Hyper-parameters using Random Search in Python

Step 1 - Import the library - RandomizedSearchCv

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

Here we have imported various modules like datasets, uniform, linear_model and RandomizedSearchCV from differnt libraries. We will understand the use of these later while using it in the in the code snipet.
For now just have a look on these imports.

Step 2 - Setup the Data

Here we have used datasets to load the inbuilt iris dataset and we have created objects X and y to store the data and the target value respectively. iris = datasets.load_iris() X = iris.data y = iris.target

Step 3 - Using Model

Here, we are using Logistic Regression as a Machine Learning model to use RandomisedSearchCV. So we have created an object Logistic. logistic = linear_model.LogisticRegression()

Step 5 - Parameters to be optimized

Logistic Regression requires two parameters "C" and "penalty" to be optimised by RandomisedSearchCV. So we have set these two parameters as a list of values form which RandomisedSearchCV will select the best value of parameter. C = uniform(loc=0, scale=4) penalty = ["l1", "l2"] hyperparameters = dict(C=C, penalty=penalty)

Step 6 - Using RandomisedSearchCV and Printing Results

Before using RandomisedSearchCV, lets have a look on the important parameters.

  • estimator: In this we have to pass the models or functions on which we want to use RandomisedSearchCV
  • param_grid: Dictionary or list of parameters of models or function in which RandomisedSearchCV have to select the best.
  • Scoring: It is used as a evaluating metric for the model performance to decide the best hyperparameters, if not especified then it uses estimator score.
Making an object clf for RandomisedSearchCV and fitting the dataset i.e X and y clf = RandomizedSearchCV(logistic, hyperparameters, random_state=1, n_iter=100, cv=5, verbose=0, n_jobs=-1) best_model = clf.fit(X, y) Now we are using print statements to print the results. It will give the values of hyperparameters as a result. print("Best Penalty:", best_model.best_estimator_.get_params()["penalty"]) print("Best C:", best_model.best_estimator_.get_params()["C"]) As an output we get:

Best Penalty: l1
Best C: 1.668088018810296

Relevant Projects

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series 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.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

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 Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

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 on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.