TransWikia.com

How to achieve SHAP values for a CatBoost model in R?

Data Science Asked on August 24, 2021

I’m asked to create a SHAP analysis in R but I cannot find it how to obtain it for a CatBoost model. I can get the SHAP values of an XGBoost model with

shap_values <- shap.values(xgb_model = model, X_train = train_X)

but not for CatBoost.

Here is the reproducible code for my CatBoost model:

library(data.table)
library(catboost)

train_example <- data.table(categorical_feature = c("a", "b", "a", "a", "b"), payment = c(244, 52352, 4235, 3422, 535), age = c(34, 27, 19, 40, 92), target = c(0,0,1,0,1))
label_values_train <- train_example$target
train_X <-train_example[, - "target"]
train_X$categorical_feature <- as.factor(train_X$categorical_feature)
# datapool for the train dataset:
pool <- catboost.load_pool(train_X, label = label_values_train, cat_features = 1)
fit_params_report <- list(iterations = 1000,
                          loss_function = 'Logloss',
                          eval_metric='F1',
                          border_count = 42,
                          depth = 8,
                          learning_rate = 0.04,
                          l2_leaf_reg = 0.001,
                          task_type = 'CPU')
 
model <- catboost.train(pool, params = fit_params_report)
  

How can I proceed to get the SHAP values here?

One Answer

catboost::catboost.get_feature_importance(model, pool = pool, type = "ShapValues")

Answered by JensMB on August 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP