.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "python/gpu-examples/tree_shap.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_python_gpu-examples_tree_shap.py: Use GPU to speedup SHAP value computation ========================================= Demonstrates using GPU acceleration to compute SHAP values for feature importance. .. GENERATED FROM PYTHON SOURCE LINES 8-56 .. code-block:: Python import shap from sklearn.datasets import fetch_california_housing import xgboost as xgb # Fetch dataset using sklearn data = fetch_california_housing() print(data.DESCR) X = data.data y = data.target num_round = 500 param = { "eta": 0.05, "max_depth": 10, "tree_method": "hist", "device": "cuda", } # GPU accelerated training dtrain = xgb.DMatrix(X, label=y, feature_names=data.feature_names) model = xgb.train(param, dtrain, num_round) # Compute shap values using GPU with xgboost model.set_param({"device": "cuda"}) shap_values = model.predict(dtrain, pred_contribs=True) # Compute shap interaction values using GPU shap_interaction_values = model.predict(dtrain, pred_interactions=True) # shap will call the GPU accelerated version as long as the device parameter is set to # "cuda" explainer = shap.TreeExplainer(model) shap_values = explainer.shap_values(X) # visualize the first prediction's explanation shap.force_plot( explainer.expected_value, shap_values[0, :], X[0, :], feature_names=data.feature_names, matplotlib=True, ) # Show a summary of feature importance shap.summary_plot(shap_values, X, plot_type="bar", feature_names=data.feature_names) .. _sphx_glr_download_python_gpu-examples_tree_shap.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: tree_shap.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: tree_shap.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: tree_shap.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_