CatBoostRegressor - Training a Regression Model With CatBoost

Python

This code snippets demonstrates how to use CatBoost for regression, how to modify its hyperparameters, how to store the trained model, how to visualize feature importance, and how to evaluate the performance of the model using various metrics

``` 1|  from catboost import CatBoostRegressor
2|  from sklearn.metrics import mean_squared_error, mean_absolute_error, max_error, explained_variance_score, mean_absolute_percentage_error
3|  import matplotlib.pyplot as plt
4|
5|  # Step 1: Initialise and fit CatBoost regression model
6|  model = CatBoostRegressor(loss_function='RMSE',
7|                            n_estimators=1000,
8|                            max_depth=4,
9|                            learning_rate=0.1,
10|                            min_child_samples=1,
11|                            colsample_bylevel=0.9,
12|                            subsample=0.9,
13|                            random_seed=101)
14|  model.fit(X_train, y_train)
15|
16|  model.save_model('catboost_regressor.model')
17|
18|  # Step 2: Plot feature importances
19|  features = X_train.columns
20|  importance_values = model.feature_importances_
21|
22|  plt.barh(y=range(len(features)),
23|           width=importance_values,
24|           tick_label=features)
25|  plt.show()
26|
27|  # Step 3: Make prediction for test data & evaluate performance
28|  y_pred = model.predict(X_test)
29|  print('RMSE:',mean_squared_error(y_test, y_pred, squared=False))
30|  print('MAE:',mean_absolute_error(y_test, y_pred))
31|  print('MAPE:',mean_absolute_percentage_error(y_test, y_pred))
32|  print('Max Error:',max_error(y_test, y_pred))
33|  print('Explained Variance Score:',explained_variance_score(y_test, y_pred))
```
Did you find this snippet useful?