MCPcopy Create free account
hub / github.com/dmlc/xgboost / shap_values

Function shap_values

python-package/xgboost/interpret.py:53–124  ·  view source on GitHub ↗

Return SHAP values for an XGBoost model. .. warning:: This function is still working in progress. This function accepts either a :py:class:`xgboost.Booster` or an sklearn-style XGBoost model and returns feature contributions together with the separated bias term. Parame

(  # pylint: disable=too-many-arguments
    model: object,
    X: Union[DMatrix, ArrayLike],
    *,
    X_background: Optional[Union[DMatrix, ArrayLike]] = None,
    output_margin: bool = False,
    iteration_range: Optional[IterationRange] = None,
    missing: Optional[FloatCompatible] = None,
    validate_features: bool = True,
)

Source from the content-addressed store, hash-verified

51
52
53def shap_values( # pylint: disable=too-many-arguments
54 model: object,
55 X: Union[DMatrix, ArrayLike],
56 *,
57 X_background: Optional[Union[DMatrix, ArrayLike]] = None,
58 output_margin: bool = False,
59 iteration_range: Optional[IterationRange] = None,
60 missing: Optional[FloatCompatible] = None,
61 validate_features: bool = True,
62) -> Tuple[np.ndarray, np.ndarray]:
63 """Return SHAP values for an XGBoost model.
64
65 .. warning::
66
67 This function is still working in progress.
68
69 This function accepts either a :py:class:`xgboost.Booster` or an sklearn-style
70 XGBoost model and returns feature contributions together with the separated
71 bias term.
72
73 Parameters
74 ----------
75 model :
76 XGBoost booster or sklearn-style XGBoost model.
77 X :
78 Input data.
79 X_background :
80 Background data for interventional SHAP values. This is reserved for a
81 future implementation and is currently unsupported.
82 output_margin :
83 Accepted for API compatibility. SHAP contributions currently correspond
84 to the model margin.
85 iteration_range :
86 Specifies which layer of trees are used in prediction.
87 missing :
88 Value in array-like ``X`` to treat as missing. When None, use the
89 model's missing value if available, otherwise ``np.nan``. This must not
90 be specified when ``X`` is already a DMatrix.
91 validate_features :
92 Validate feature names between the model and input data.
93
94 Returns
95 -------
96 values, bias :
97 ``values`` contains feature SHAP values with the bias term removed.
98 ``bias`` contains the separated bias term. For multi-target models, the
99 output shape follows the corresponding prediction shape with the final
100 feature dimension split into ``values`` and ``bias``.
101
102 Notes
103 -----
104 To use GPU algorithms, configure the model before calling this function, for
105 example with ``booster.set_param({"device": "cuda"})``.
106 """
107 if X_background is not None:
108 raise NotImplementedError("`X_background` is not yet supported.")
109 # SHAP contributions currently correspond to the model margin. Keep this
110 # argument in the initial API so callers can use the proposed signature.

Callers 2

interaction_valuesMethod · 0.85

Calls 4

_as_boosterFunction · 0.85
_as_prediction_dmatrixFunction · 0.85
_get_iteration_rangeFunction · 0.85
predictMethod · 0.45

Tested by 2

interaction_valuesMethod · 0.68