Optimal parameters for CatBoost using GridSearchCV in Python

In this recipe which shows how we can optimize parameters for CatBoost using the GridSearchCV also how we can perform validation dataset from the existing dataset and how to apply CatBoost Classifier is shown in this recipe.

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. Grid Search passes all 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.

This python source code does the following:
1. pip installs Catboost
2. Imports SKlearn dataset
3. Performs validation dataset from the existing dataset
4. Applies Catboost Classifier
5. Hyperparameter tuning using GridSearchCV

So this recipe is a short example of how we can find optimal parameters using GridSearchCV.

Get Closer To Your Dream of Becoming a Data Scientist with 70+ Solved End-to-End ML Projects

Step 1 - Import the library - GridSearchCv

from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.model_selection import GridSearchCV from catboost import CatBoostClassifier

Here we have imported various modules like datasets, CatBoostClassifier and GridSearchCV 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. dataset = datasets.load_iris() X = dataset.data; y = dataset.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)

Step 3 - Model and its Parameter

Here, we are using CatBoostClassifier as a Machine Learning model to use GridSearchCV. So we have created an object CBC. CBC = CatBoostClassifier() Now we have defined the parameters of the model which we want to pass to through GridSearchCV to get the best parameters. So we are making an dictionary called parameters in which we have four parameters learning_rate, subsample, n_estimators and max_depth. parameters = {'depth' : [4,5,6,7,8,9, 10], 'learning_rate' : [0.01,0.02,0.03,0.04], 'iterations' : [10, 20,30,40,50,60,70,80,90, 100] }

 

Explore More Data Science and Machine Learning Projects for Practice. Fast-Track Your Career Transition with ProjectPro

Step 4 - Using GridSearchCV and Printing Results

Before using GridSearchCV, 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 GridSearchCV
  • param_grid: Dictionary or list of parameters of models or function in which GridSearchCV 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.
  • cv : In this we have to pass a interger value, as it signifies the number of splits that is needed for cross validation. By default is set as five.
  • n_jobs : This signifies the number of jobs to be run in parallel, -1 signifies to use all processor.

Making an object grid_GBC for GridSearchCV and fitting the dataset i.e X and y Grid_CBC = GridSearchCV(estimator=CBC, param_grid = parameters, cv = 2, n_jobs=-1) Grid_CBC.fit(X_train, y_train) Now we are using print statements to print the results. It will give the values of hyperparameters as a result. print(" Results from Grid Search " ) print("\n The best estimator across ALL searched params:\n",Grid_CBC.best_estimator_) print("\n The best score across ALL searched params:\n",Grid_CBC.best_score_) print("\n The best parameters across ALL searched params:\n",Grid_CBC.best_params_) As an output we get:

0:	learn: 1.0891400	total: 2.94ms	remaining: 85.2ms
1:	learn: 1.0783511	total: 4.32ms	remaining: 60.5ms
2:	learn: 1.0694444	total: 6.3ms	remaining: 56.7ms
3:	learn: 1.0595396	total: 8.25ms	remaining: 53.6ms
4:	learn: 1.0503198	total: 9.34ms	remaining: 46.7ms
5:	learn: 1.0410468	total: 11.2ms	remaining: 44.7ms
6:	learn: 1.0321956	total: 13ms	remaining: 42.7ms
7:	learn: 1.0243880	total: 14.9ms	remaining: 41ms
8:	learn: 1.0171330	total: 16.9ms	remaining: 39.4ms
9:	learn: 1.0084122	total: 17.4ms	remaining: 34.8ms
10:	learn: 0.9976315	total: 18.5ms	remaining: 31.9ms
11:	learn: 0.9901578	total: 20.4ms	remaining: 30.6ms
12:	learn: 0.9800001	total: 20.9ms	remaining: 27.4ms
13:	learn: 0.9695078	total: 21.3ms	remaining: 24.3ms
14:	learn: 0.9621747	total: 23.2ms	remaining: 23.2ms
15:	learn: 0.9554019	total: 25.1ms	remaining: 22ms
16:	learn: 0.9474079	total: 27ms	remaining: 20.7ms
17:	learn: 0.9414282	total: 28.9ms	remaining: 19.3ms
18:	learn: 0.9321771	total: 29.4ms	remaining: 17ms
19:	learn: 0.9257181	total: 31.3ms	remaining: 15.6ms
20:	learn: 0.9168876	total: 31.8ms	remaining: 13.6ms
21:	learn: 0.9105180	total: 33.7ms	remaining: 12.3ms
22:	learn: 0.9035570	total: 35.7ms	remaining: 10.9ms
23:	learn: 0.8961079	total: 36.4ms	remaining: 9.09ms
24:	learn: 0.8890353	total: 38.3ms	remaining: 7.66ms
25:	learn: 0.8826284	total: 40.2ms	remaining: 6.18ms
26:	learn: 0.8734174	total: 40.6ms	remaining: 4.52ms
27:	learn: 0.8647497	total: 41ms	remaining: 2.93ms
28:	learn: 0.8583818	total: 42.9ms	remaining: 1.48ms
29:	learn: 0.8525250	total: 44.9ms	remaining: 0us

Results from Grid Search 

 The best estimator across ALL searched params:
 

 The best score across ALL searched params:
 0.9620827285921625

 The best parameters across ALL searched params:
 {'depth': 9, 'iterations': 30, 'learning_rate': 0.01}



Join Millions of Satisfied Developers and Enterprises to Maximize Your Productivity and ROI with ProjectPro - Read ProjectPro Reviews Now!  

Download Materials

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

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

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Build a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN

Build a Speech-Text Transcriptor with Nvidia Quartznet Model
In this Deep Learning Project, you will leverage transfer learning from Nvidia QuartzNet pre-trained models to develop a speech-to-text transcriptor.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.