Skip to content

Instantly share code, notes, and snippets.

@matejsarlija
Created December 13, 2020 09:56
Show Gist options
  • Save matejsarlija/9484e88f6af012f0f9286d2f5057ab4c to your computer and use it in GitHub Desktop.
Save matejsarlija/9484e88f6af012f0f9286d2f5057ab4c to your computer and use it in GitHub Desktop.
Find optimal parameters for CatBoost using GridSearchCV for Regression in Python
"""source : https://nilimeshhalder.medium.com/how-to-find-optimal-parameters-for-catboost-using-gridsearchcv-for-regression-in-python-ef778b60d95d"""
def Snippet_199():
print()
print(format('How to find optimal parameters for CatBoost using GridSearchCV for Regression','*^82'))
import warnings
warnings.filterwarnings("ignore")
# load libraries
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from catboost import CatBoostRegressor
# load the iris datasets
dataset = datasets.load_boston()
X = dataset.data; y = dataset.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
model = CatBoostRegressor()
parameters = {'depth' : [6,8,10],
'learning_rate' : [0.01, 0.05, 0.1],
'iterations' : [30, 50, 100]
}
grid = GridSearchCV(estimator=model, param_grid = parameters, cv = 2, n_jobs=-1)
grid.fit(X_train, y_train)
# Results from Grid Search
print("\n========================================================")
print(" Results from Grid Search " )
print("========================================================")
print("\n The best estimator across ALL searched params:\n",
grid.best_estimator_)
print("\n The best score across ALL searched params:\n",
grid.best_score_)
print("\n The best parameters across ALL searched params:\n",
grid.best_params_)
print("\n ========================================================")
Snippet_199()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment