lightgbm.unify {treeshap}R Documentation

Unify LightGBM model

Description

Convert your LightGBM model into a standardized representation. The returned representation is easy to be interpreted by the user and ready to be used as an argument in treeshap() function.

Usage

lightgbm.unify(lgb_model, data, recalculate = FALSE)

Arguments

lgb_model

A lightgbm model - object of class lgb.Booster

data

Reference dataset. A data.frame or matrix with the same columns as in the training set of the model. Usually dataset used to train model.

recalculate

logical indicating if covers should be recalculated according to the dataset given in data. Keep it FALSE if training data are used.

Value

a unified model representation - a model_unified.object object

See Also

gbm.unify for GBM models

xgboost.unify for XGBoost models

ranger.unify for ranger models

randomForest.unify for randomForest models

Examples


library(lightgbm)
param_lgbm <- list(objective = "regression", max_depth = 2,
                   force_row_wise = TRUE, num_iterations = 20)
data_fifa <- fifa20$data[!colnames(fifa20$data) %in%
             c('work_rate', 'value_eur', 'gk_diving', 'gk_handling',
             'gk_kicking', 'gk_reflexes', 'gk_speed', 'gk_positioning')]
data <- na.omit(cbind(data_fifa, fifa20$target))
sparse_data <- as.matrix(data[,-ncol(data)])
x <- lightgbm::lgb.Dataset(sparse_data, label = as.matrix(data[,ncol(data)]))
lgb_data <- lightgbm::lgb.Dataset.construct(x)
lgb_model <- lightgbm::lightgbm(data = lgb_data, params = param_lgbm,
                                verbose = -1, num_threads = 0)
unified_model <- lightgbm.unify(lgb_model, sparse_data)
shaps <- treeshap(unified_model, data[1:2, ])
plot_contribution(shaps, obs = 1)


[Package treeshap version 0.3.1 Index]