.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/trees/plot_treeshapiq_custom_tree.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_auto_examples_trees_plot_treeshapiq_custom_tree.py: TreeSHAP-IQ for Custom Tree Models =================================== This example demonstrates how to use :class:`~shapiq.TreeExplainer` to explain a custom tree model built from scratch using the Play Tennis dataset. .. GENERATED FROM PYTHON SOURCE LINES 8-15 .. code-block:: Python from __future__ import annotations import numpy as np import shapiq .. GENERATED FROM PYTHON SOURCE LINES 16-20 The Play Tennis Dataset ----------------------- A classic dataset with 4 features (Outlook, Temperature, Humidity, Wind) and a binary target (Play Tennis: Yes/No). Features are numerically encoded. .. GENERATED FROM PYTHON SOURCE LINES 20-41 .. code-block:: Python X = np.array( [ [1, 1, 1, 1], [1, 1, 1, 2], [2, 1, 1, 1], [3, 2, 1, 1], [3, 3, 2, 1], [3, 3, 2, 2], [2, 3, 2, 2], [1, 2, 1, 1], [1, 3, 2, 1], [3, 2, 2, 1], [1, 2, 2, 2], [2, 2, 1, 2], [2, 1, 2, 1], [3, 2, 1, 2], ] ) y = np.array([0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0]) .. GENERATED FROM PYTHON SOURCE LINES 42-55 Define a Custom Tree Model --------------------------- We define a 3-node decision tree: .. code-block:: text #0: Outlook / \ #1: 0 #2: Humidity / \ #3: 0 #4: Wind / \ #5: 0 #6: 1 .. GENERATED FROM PYTHON SOURCE LINES 55-66 .. code-block:: Python tree = shapiq.tree.TreeModel( children_left=np.array([1, -1, 3, -1, 5, -1, -1]), children_right=np.array([2, -1, 4, -1, 6, -1, -1]), children_missing=np.array([-1, -1, -1, -1, -1, -1, -1]), features=np.array([0, -2, 2, -2, 3, -2, -2]), thresholds=np.array([2.5, -2, 1.5, -2, 1.5, -2, -2]), values=np.array([0.5, 0.0, 0.5, 0.0, 0.0, 0.0, 1.0]), node_sample_weight=np.array([14, 5, 9, 5, 4, 1, 3]), ) .. GENERATED FROM PYTHON SOURCE LINES 67-70 Explain with TreeSHAP-IQ ------------------------- Compute exact Shapley values for a single instance. .. GENERATED FROM PYTHON SOURCE LINES 70-75 .. code-block:: Python explainer = shapiq.TreeExplainer(model=tree, index="SV", max_order=1) sv = explainer.explain(X[5]) print(sv) sv.plot_force(feature_names=["Outlook", "Temperature", "Humidity", "Wind"]) .. image-sg:: /auto_examples/trees/images/sphx_glr_plot_treeshapiq_custom_tree_001.png :alt: plot treeshapiq custom tree :srcset: /auto_examples/trees/images/sphx_glr_plot_treeshapiq_custom_tree_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none InteractionValues( index=SV, max_order=1, min_order=0, estimated=True, estimation_budget=None, n_players=4, baseline_value=0.21428571428571427, Top 10 interactions: (2,): 0.40343915343915326 (0,): 0.22982804232804221 (): 0.21428571428571427 (3,): 0.1524470899470899 (1,): 0.0 ) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.123 seconds) .. _sphx_glr_download_auto_examples_trees_plot_treeshapiq_custom_tree.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_treeshapiq_custom_tree.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_treeshapiq_custom_tree.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_treeshapiq_custom_tree.zip `