3 Upvotes

Train Catboost Classifier with GridSearch

Python
Supervised Learning

Catboostclassifier Python example with hyper parameter tuning. In this code snippet we train a classification model using Catboost. We initiate the model and then use grid search to to find optimum parameter values from a list that we define inside the grid dictionary. The model is then fit with these parameters assigned.

import catboost as cb

#Create datasets
train_dataset = cb.Pool(X_train,y_train, cat_features=categorical_indicies)
eval_dataset = cb.Pool(X_val,y_val, cat_features=categorical_indicies)

model = cb.CatBoostClassifier(iterations=1000, 
                              loss_function='Logloss', 
                              eval_metric='Accuracy')

#Declare parameters to tune and values to try
grid = {'learning_rate': [0.03, 0.1],
        'depth': [4, 6, 10],
        'l2_leaf_reg': [1, 3, 5,]}

#Find optimum parameters
model.grid_search(grid,train_dataset,plot=True)

#Fit model with early stopping if improvement hasn't been made within 50 iterations
model.fit(train_dataset, 
          eval_set=eval_dataset,
          early_stopping_rounds=50,
          plot=True,
          silent=False)

By analyseup - Last Updated Jan. 10, 2022, 11:28 p.m.

Did you find this snippet useful?

Sign up to bookmark this in your snippet library

COMMENTS
RELATED SNIPPETS
Top Contributors
75