# TreeExplainer

### *class* cuml.explainer.TreeExplainer(model, , data=None)

Model explainer that calculates Shapley values for the predictions of
tree-based models. Shapley values are a method of attributing various input
features to a given model prediction.

Uses GPUTreeShap [[1]](#rcbb0b62705ba-1) as a back-end to accelerate computation using GPUs.

Different variants of Shapley values exist based on different
interpretations of marginalising out (or conditioning on) features. For the
“tree_path_dependent” approach, see [[2]](#rcbb0b62705ba-2).

For the “interventional” approach, see [[3]](#rcbb0b62705ba-3).

We also provide two variants of feature interactions. For the
“shapley-interactions” variant of interactions, see [[2]](#rcbb0b62705ba-2), for
the “shapley-taylor” variant, see [[4]](#rcbb0b62705ba-4).

* **Parameters:**
  **model**
  : The tree based machine learning model. XGBoost, LightGBM, cuml random
    forest and sklearn random forest models are supported. Categorical
    features in XGBoost or LightGBM models are natively supported.

  **data**
  : Optional background dataset to use for marginalising out features.
    If this argument is supplied, an “interventional” approach is used.
    Computation time increases with the size of this background data set,
    consider starting with between 100-1000 examples. If this argument is
    not supplied, statistics from the tree model are used to marginalise
    out features (“tree_path_dependent”).
* **Attributes:**
  [`expected_value`](#cuml.explainer.TreeExplainer.expected_value)
  : expected_value: object

### Methods

| [`shap_interaction_values`](#cuml.explainer.TreeExplainer.shap_interaction_values)(self, X[, method])   | Estimate the SHAP interaction values for a set of samples.   |
|-----------------------------------------------------------------------------------------------|--------------------------------------------------------------|
| [`shap_values`](#cuml.explainer.TreeExplainer.shap_values)(self, X)                         | Estimate the SHAP values for a set of samples.               |

### Examples

```python
>>> import numpy as np
>>> import cuml
>>> from cuml.explainer import TreeExplainer
>>> X = np.array([[0.0, 2.0], [1.0, 0.5]])
>>> y = np.array([0, 1])
>>> model = cuml.ensemble.RandomForestRegressor().fit(X, y)
>>> explainer = TreeExplainer(model=model)
>>> shap_values = explainer.shap_values(X)
```

<!-- !! processed by numpydoc !! -->

#### expected_value

expected_value: object

<!-- !! processed by numpydoc !! -->

#### shap_interaction_values(self, X, method='shapley-interactions')[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/explainer/tree_shap.pyx#L289)

Estimate the SHAP interaction values for a set of samples. For a
given row, the SHAP values plus the [`expected_value`](#cuml.explainer.TreeExplainer.expected_value) attribute sum
up to the raw model prediction. ‘Raw model prediction’ means before
the application of a link function, for example, the SHAP values of
an XGBoost binary classification are in the additive logit space as
opposed to probability space.

Interventional feature marginalisation is not supported.

* **Parameters:**
  **X**
  : A matrix of samples (# samples x # features) on which to explain
    the model’s output.

  **method**
  : One of [‘shapley-interactions’, ‘shapley-taylor’]
* **Returns:**
  array
  : Returns a matrix of SHAP values of shape
    (# classes x # samples x # features x # features).

<!-- !! processed by numpydoc !! -->

#### shap_values(self, X)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/explainer/tree_shap.pyx#L215)

Estimate the SHAP values for a set of samples. For a given row, the
SHAP values plus the [`expected_value`](#cuml.explainer.TreeExplainer.expected_value) attribute sum up to the raw
model prediction. ‘Raw model prediction’ means before the application
of a link function, for example, the SHAP values of an XGBoost binary
classification will be in the additive logit space as opposed to
probability space.

* **Parameters:**
  **X**
  : A matrix of samples (# samples x # features) on which to explain
    the model’s output.
* **Returns:**
  array
  : Returns a matrix of SHAP values of shape
    (# classes x # samples x # features).

<!-- !! processed by numpydoc !! -->
