.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_examples\matplotlib\plot_main30_boxplot_sepsis_shap.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr__examples_matplotlib_plot_main30_boxplot_sepsis_shap.py: 11. Visualizing SHAP across timesteps ========================================================= This script analyzes the feature importance from a time-series model by visualizing pre-computed SHAP values. This script loads SHAP (SHapley Additive exPlanations) values from a CSV file to explore feature importance in a temporal context. After filtering for a predefined list of medical features, it generates three distinct Seaborn plots: a boxenplot, a violin plot, and a standard boxplot. Each plot visualizes the distribution of SHAP values for every feature across multiple timesteps. The main objective is to identify which features have the most significant impact on the model's predictions (indicated by higher SHAP values) and to observe how this influence evolves over time. .. note:: See plotly example, were interaction with data is possible! .. GENERATED FROM PYTHON SOURCE LINES 21-126 .. rst-class:: sphx-glr-horizontal * .. image-sg:: /_examples/matplotlib/images/sphx_glr_plot_main30_boxplot_sepsis_shap_001.png :alt: plot main30 boxplot sepsis shap :srcset: /_examples/matplotlib/images/sphx_glr_plot_main30_boxplot_sepsis_shap_001.png :class: sphx-glr-multi-img * .. image-sg:: /_examples/matplotlib/images/sphx_glr_plot_main30_boxplot_sepsis_shap_002.png :alt: plot main30 boxplot sepsis shap :srcset: /_examples/matplotlib/images/sphx_glr_plot_main30_boxplot_sepsis_shap_002.png :class: sphx-glr-multi-img * .. image-sg:: /_examples/matplotlib/images/sphx_glr_plot_main30_boxplot_sepsis_shap_003.png :alt: plot main30 boxplot sepsis shap :srcset: /_examples/matplotlib/images/sphx_glr_plot_main30_boxplot_sepsis_shap_003.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Unnamed: 0 sample timestep features feature_values shap_values 0 0 0 0 Ward Lactate 0.0 0.000652 4 4 0 0 Platelets 0.0 -0.001705 5 5 0 0 Haemoglobin 0.0 -0.000918 6 6 0 0 Mean cell volume, blood 0.0 -0.000654 7 7 0 0 Haematocrit 0.0 -0.000487 16 16 0 0 Neutrophils 0.0 0.002521 17 17 0 0 Chloride 0.0 -0.000858 18 18 0 0 Lymphocytes 0.0 -0.002920 19 19 0 0 Monocytes 0.0 -0.002224 20 20 0 0 Eosinophils 0.0 -0.005246 C:\Users\kelda\Desktop\repositories\github\python-spare-code\main\examples\matplotlib\plot_main30_boxplot_sepsis_shap.py:125: UserWarning: FigureCanvasAgg is non-interactive, and thus cannot be shown | .. code-block:: default :lineno-start: 22 # Libraries import seaborn as sns import pandas as pd import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt from scipy import stats from matplotlib.colors import LogNorm sns.set_theme(style="white") # See https://matplotlib.org/devdocs/users/explain/customizing.html mpl.rcParams['axes.titlesize'] = 8 mpl.rcParams['axes.labelsize'] = 8 mpl.rcParams['xtick.labelsize'] = 8 mpl.rcParams['ytick.labelsize'] = 8 mpl.rcParams['legend.fontsize'] = 7 mpl.rcParams['legend.handlelength'] = 1 mpl.rcParams['legend.handleheight'] = 1 mpl.rcParams['legend.loc'] = 'upper left' # Features features = [ 'Ward Lactate', #'Ward Glucose', #'Ward sO2', #'White blood cell count, blood', 'Platelets', 'Haemoglobin', 'Mean cell volume, blood', 'Haematocrit', #'Mean cell haemoglobin conc, blood', #'Mean cell haemoglobin level, blood', #'Red blood cell count, blood', #'Red blood cell distribution width', #'Creatinine', #'Urea level, blood', #'Potassium', #'Sodium', 'Neutrophils', 'Chloride', 'Lymphocytes', 'Monocytes', 'Eosinophils', 'C-Reactive Protein', 'Albumin', #'Alkaline Phosphatase', #'Glucose POCT Strip Blood', 'Total Protein', 'Globulin', 'Alanine Transaminase', 'Bilirubin', 'Prothrombin time', 'Fibrinogen (clauss)', 'Procalcitonin', 'Ferritin', 'D-Dimer', 'sex', 'age' ] # Load data data = pd.read_csv('../../datasets/shap/shap.csv') # Filter data = data[data.features.isin(features)] # Show print(data.head(10)) # .. todo:: Change flier size, cmap, ... def configure_ax(ax): sns.despine(ax=ax) lg = ax.legend(loc='upper center', bbox_to_anchor=(0.05, 1.15, 0.9, 0.1), borderaxespad=2, ncol=5, mode='expand') plt.tight_layout() # Boxenplot plt.figure(figsize=(12, 4)) ax = sns.boxenplot(data, x='timestep', y='shap_values', hue='features', saturation=0.5, showfliers=False) configure_ax(ax) # Violinplot plt.figure(figsize=(12, 4)) ax = sns.violinplot(data, x='timestep', y='shap_values', hue='features', saturation=0.5) configure_ax(ax) # Boxplot plt.figure(figsize=(12, 4)) ax = sns.boxplot(data, x='timestep', y='shap_values', hue='features', saturation=0.5, showfliers=False, whis=1.0) configure_ax(ax) # Show plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 5.925 seconds) .. _sphx_glr_download__examples_matplotlib_plot_main30_boxplot_sepsis_shap.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_main30_boxplot_sepsis_shap.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_main30_boxplot_sepsis_shap.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_