30. Sample shap.csv boxplot

The aim is to visualise all the features for all the timesteps to quickly see which shap values are higher and therefore influence more in the result.

 11 # Libraries
 12 import seaborn as sns
 13 import pandas as pd
 14 import numpy as np
 15 import matplotlib as mpl
 16 import matplotlib.pyplot as plt
 17 import plotly.express as px
 18
 19 from plotly.io import show
 20 from plotly.colors import n_colors
 21 from plotly.express.colors import sample_colorscale
 22
 23 # See https://matplotlib.org/devdocs/users/explain/customizing.html
 24 mpl.rcParams['axes.titlesize'] = 8
 25 mpl.rcParams['axes.labelsize'] = 8
 26 mpl.rcParams['xtick.labelsize'] = 8
 27 mpl.rcParams['ytick.labelsize'] = 8
 28 mpl.rcParams['legend.fontsize'] = 7
 29 mpl.rcParams['legend.handlelength'] = 1
 30 mpl.rcParams['legend.handleheight'] = 1
 31 mpl.rcParams['legend.loc'] = 'upper left'
 32
 33 try:
 34     __file__
 35     TERMINAL = True
 36 except:
 37     TERMINAL = False
 38
 39
 40 # Load data
 41 data = pd.read_csv('../../datasets/shap/shap.csv')
 42
 43 # Show
 44 if TERMINAL:
 45     print("\nData:")
 46     print(data)
 47 data
 48
 49 # Number of colors
 50 N = data.features.nunique()
 51
 52 # see https://plotly.com/python/builtin-colorscales/#discrete-color-sequences
 53 # see https://plotly.github.io/plotly.py-docs/generated/plotly.express.box.html
 54
 55 # generate an array of rainbow colors by fixing the saturation and lightness of the
 56 # HSL representation of colour and marching around the hue. Plotly accepts any CSS
 57 # color format, see e.g. http://www.w3schools.com/cssref/css_colors_legal.asp.
 58 c0 = ['hsl('+str(h)+',50%'+',50%)'
 59     for h in np.linspace(0, 360, N)]
 60
 61 # More colors
 62 x = np.linspace(0, 1, N)
 63 c1 = sample_colorscale('viridis', list(x))
 64 c2 = sample_colorscale('RdBu', list(x))
 65 c3 = sample_colorscale('Jet', list(x))
 66 c4 = sample_colorscale('Agsunset', list(x))
 67
 68 # .. note:: Remove width and size if running locally.
 69
 70 # Boxplot
 71 fig = px.box(data, x='timestep', y='shap_values',
 72              color='features', color_discrete_sequence=c4,
 73              points='outliers', width=750, height=900)
 74
 75 # .. note:: If using widescreen, commenting the legend section
 76 #           will automatically generate a vertical legend with
 77 #           scrolling if needed. For display purposes in the
 78 #           docs we have included the legend on top.
 79
 80 # Update layout
 81 fig.update_layout(
 82     #margin={
 83     #    'l': 0,
 84     #    'r': 0,
 85     #    'b': 0,
 86     #    't': 0,
 87     #    'pad': 4
 88     #},
 89     legend=dict(
 90         orientation="h",
 91         entrywidth=140,
 92         yanchor="bottom",
 93         y=1.02,
 94         xanchor="right",
 95         x=1,
 96         #font=dict(
 97         #    family="Courier",
 98         #    size=7,
 99         #    #color="black"
100         #),
101     ),
102     paper_bgcolor='rgba(0,0,0,0)',  # transparent
103     plot_bgcolor='rgba(0,0,0,0)'  # transparent
104 )
105
106 # Update xaxis
107 fig.update_xaxes(
108     mirror=False,
109     ticks='outside',
110     showline=False,
111     linecolor='black',
112     gridcolor='lightgrey'
113 )
114
115 # Update yaxis
116 fig.update_yaxes(
117     mirror=False,
118     ticks='outside',
119     showline=False,
120     linecolor='black',
121     gridcolor='lightgrey'
122 )
123
124 # Show
125 show(fig)

Total running time of the script: ( 0 minutes 3.220 seconds)

Gallery generated by Sphinx-Gallery