.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_examples/shap/plot_main05.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_shap_plot_main05.py: 05. Basic example ================= .. GENERATED FROM PYTHON SOURCE LINES 5-144 .. code-block:: default :lineno-start: 6 # Libraries import shap import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import matplotlib as mpl import matplotlib.colorbar import matplotlib.colors import matplotlib.cm from mpl_toolkits.axes_grid1 import make_axes_locatable try: __file__ TERMINAL = True except: TERMINAL = False # ------------------------ # Methods # ------------------------ def scalar_colormap(values, cmap, vmin, vmax): """This method creates a colormap based on values. Parameters ---------- values : array-like The values to create the corresponding colors cmap : str The colormap vmin, vmax : float The minimum and maximum possible values Returns ------- scalar colormap """ # Create scalar mappable norm = mpl.colors.Normalize(vmin=vmin, vmax=vmax, clip=True) mapper = mpl.cm.ScalarMappable(norm=norm, cmap=cmap) # Get color map colormap = sns.color_palette([mapper.to_rgba(i) for i in values]) # Return return colormap, norm def scalar_palette(values, cmap, vmin, vmax): """This method creates a colorpalette based on values. Parameters ---------- values : array-like The values to create the corresponding colors cmap : str The colormap vmin, vmax : float The minimum and maximum possible values Returns ------- scalar colormap """ # Create a matplotlib colormap from name #cmap = sns.light_palette(cmap, reverse=False, as_cmap=True) cmap = sns.color_palette(cmap, as_cmap=True) # Normalize to the range of possible values from df["c"] norm = mpl.colors.Normalize(vmin=vmin, vmax=vmax) # Create a color dictionary (value in c : color from colormap) colors = {} for cval in values: colors.update({cval : cmap(norm(cval))}) # Return return colors, norm def create_random_shap(samples, timesteps, features): """Create random LSTM data. .. note: No need to create the 3D matrix and then reshape to 2D. It would be possible to create directly the 2D matrix. Parameters ---------- samples: int The number of observations timesteps: int The number of time steps features: int The number of features Returns ------- Stacked matrix with the data. """ # .. note: Either perform a pre-processing step such as # normalization or generate the features within # the appropriate interval. # Create dataset x = np.random.randint(low=0, high=100, size=(samples, timesteps, features)) y = np.random.randint(low=0, high=2, size=samples).astype(float) i = np.vstack(np.dstack(np.indices((samples, timesteps)))) # Create DataFrame df = pd.DataFrame( data=np.hstack((i, x.reshape((-1, features)))), columns=['sample', 'timestep'] + \ ['f%s'%j for j in range(features)] ) df_stack = df.set_index(['sample', 'timestep']).stack() df_stack = df_stack df_stack.name = 'shap_values' df_stack = df_stack.to_frame() df_stack.index.names = ['sample', 'timestep', 'features'] df_stack = df_stack.reset_index() df_stack['feature_values'] = np.random.randint( low=0, high=100, size=df_stack.shape[0]) return df_stack def load_shap_file(): data = pd.read_csv('./data/shap.csv') data = data.iloc[: , 1:] #data.timestep = data.timestep - (data.timestep.nunique() - 1) return data .. GENERATED FROM PYTHON SOURCE LINES 145-146 Lets generate and/or load the shap values. .. GENERATED FROM PYTHON SOURCE LINES 146-172 .. code-block:: default :lineno-start: 147 # .. note: The right format to use for plotting depends # on the library we use. The data structure is # good when using seaborn # Load data data = create_random_shap(10, 6, 4) #data = load_shap_file() #data = data[data['sample'] < 100] shap_values = pd.pivot_table(data, values='shap_values', index=['sample', 'timestep'], columns=['features']) feature_values = pd.pivot_table(data, values='feature_values', index=['sample', 'timestep'], columns=['features']) # Show if TERMINAL: print("\nShow:") print(data) print(shap_values) print(feature_values) .. GENERATED FROM PYTHON SOURCE LINES 173-174 Let's see how data looks like .. GENERATED FROM PYTHON SOURCE LINES 174-176 .. code-block:: default :lineno-start: 174 data.head(10) .. raw:: html
sample timestep features shap_values feature_values
0 0 0 f0 51 25
1 0 0 f1 39 44
2 0 0 f2 73 58
3 0 0 f3 41 52
4 0 1 f0 28 91
5 0 1 f1 30 50
6 0 1 f2 32 29
7 0 1 f3 91 32
8 0 2 f0 26 54
9 0 2 f1 60 73


.. GENERATED FROM PYTHON SOURCE LINES 177-178 Let's see how shap_values looks like .. GENERATED FROM PYTHON SOURCE LINES 178-180 .. code-block:: default :lineno-start: 178 shap_values.iloc[:10, :5] .. raw:: html
features f0 f1 f2 f3
sample timestep
0 0 51 39 73 41
1 28 30 32 91
2 26 60 6 25
3 80 42 59 29
4 25 40 64 6
5 58 19 84 91
1 0 68 92 75 12
1 94 60 29 92
2 12 13 58 18
3 84 15 80 55


.. GENERATED FROM PYTHON SOURCE LINES 181-182 Let's see how feature_values looks like .. GENERATED FROM PYTHON SOURCE LINES 182-185 .. code-block:: default :lineno-start: 182 feature_values.iloc[:10, :5] .. raw:: html
features f0 f1 f2 f3
sample timestep
0 0 25 44 58 52
1 91 50 29 32
2 54 73 56 30
3 93 17 45 44
4 34 60 40 58
5 89 12 55 82
1 0 52 9 80 58
1 24 47 5 28
2 11 53 29 25
3 36 60 57 2


.. GENERATED FROM PYTHON SOURCE LINES 186-190 Display using ``shap.summary_plot`` ----------------------------------------------- The first option is to use the ``shap`` library to plot the results. .. GENERATED FROM PYTHON SOURCE LINES 190-199 .. code-block:: default :lineno-start: 191 # Let's define/extract some useful variables. N = 4 # max loops filter TIMESTEPS = len(shap_values.index.unique(level='timestep')) # number of timesteps SAMPLES = len(shap_values.index.unique(level='sample')) # number of samples shap_min = data.shap_values.min() shap_max = data.shap_values.max() .. GENERATED FROM PYTHON SOURCE LINES 200-201 Now, let's display the shap values for all features in each timestep. .. GENERATED FROM PYTHON SOURCE LINES 201-224 .. code-block:: default :lineno-start: 202 # For each timestep (visualise all features) for i, step in enumerate(range(TIMESTEPS)[:N]): # Show #print('%2d. %s' % (i, step)) # .. note: First option (commented) is only necessary if we work # with a numpy array. However, since we are using a DataFrame # with the timestep, we can index by that index level. # Compute indices #indice = np.arange(SAMPLES)*TIMESTEPS + step indice = shap_values.index.get_level_values('timestep') == i # Create auxiliary matrices shap_aux = shap_values.iloc[indice] feat_aux = feature_values.iloc[indice] # Display plt.figure() plt.title("Timestep: %s" % i) shap.summary_plot(shap_aux.to_numpy(), feat_aux, show=False) plt.xlim(shap_min, shap_max) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /_examples/shap/images/sphx_glr_plot_main05_001.png :alt: Timestep: 0 :srcset: /_examples/shap/images/sphx_glr_plot_main05_001.png :class: sphx-glr-multi-img * .. image-sg:: /_examples/shap/images/sphx_glr_plot_main05_002.png :alt: Timestep: 1 :srcset: /_examples/shap/images/sphx_glr_plot_main05_002.png :class: sphx-glr-multi-img * .. image-sg:: /_examples/shap/images/sphx_glr_plot_main05_003.png :alt: Timestep: 2 :srcset: /_examples/shap/images/sphx_glr_plot_main05_003.png :class: sphx-glr-multi-img * .. image-sg:: /_examples/shap/images/sphx_glr_plot_main05_004.png :alt: Timestep: 3 :srcset: /_examples/shap/images/sphx_glr_plot_main05_004.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 225-226 Now, let's display the shap values for all timesteps of each feature. .. GENERATED FROM PYTHON SOURCE LINES 226-247 .. code-block:: default :lineno-start: 227 # For each feature (visualise all time-steps) for i, f in enumerate(shap_values.columns[:N]): # Show #print('%2d. %s' % (i, f)) # Create auxiliary matrices (select feature and reshape) shap_aux = shap_values.iloc[:, i] \ .to_numpy().reshape(-1, TIMESTEPS) feat_aux = feature_values.iloc[:, i] \ .to_numpy().reshape(-1, TIMESTEPS) feat_aux = pd.DataFrame(feat_aux, columns=['timestep %s'%j for j in range(TIMESTEPS)] ) # Show plt.figure() plt.title("Feature: %s" % f) shap.summary_plot(shap_aux, feat_aux, sort=False, show=False) plt.xlim(shap_min, shap_max) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /_examples/shap/images/sphx_glr_plot_main05_005.png :alt: Feature: f0 :srcset: /_examples/shap/images/sphx_glr_plot_main05_005.png :class: sphx-glr-multi-img * .. image-sg:: /_examples/shap/images/sphx_glr_plot_main05_006.png :alt: Feature: f1 :srcset: /_examples/shap/images/sphx_glr_plot_main05_006.png :class: sphx-glr-multi-img * .. image-sg:: /_examples/shap/images/sphx_glr_plot_main05_007.png :alt: Feature: f2 :srcset: /_examples/shap/images/sphx_glr_plot_main05_007.png :class: sphx-glr-multi-img * .. image-sg:: /_examples/shap/images/sphx_glr_plot_main05_008.png :alt: Feature: f3 :srcset: /_examples/shap/images/sphx_glr_plot_main05_008.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 248-250 .. note:: If y-axis represents timesteps the ``sort`` parameter in the ``summary_plot`` function is set to False. .. GENERATED FROM PYTHON SOURCE LINES 252-260 Display using ``sns.stripplot`` ------------------------------- .. warning:: This method seems to be quite slow. Let's display the shap values for each feature and all time steps. In contrast to the previous example, the timesteps are now displayed on the x-axis and the y-axis contains the shap values. .. GENERATED FROM PYTHON SOURCE LINES 260-307 .. code-block:: default :lineno-start: 262 def add_colorbar(fig, cmap, norm): """""" divider = make_axes_locatable(plt.gca()) ax_cb = divider.new_horizontal(size="5%", pad=0.05) fig.add_axes(ax_cb) cb1 = matplotlib.colorbar.ColorbarBase(ax_cb, cmap=cmap, norm=norm, orientation='vertical') # Loop for i, (name, df) in enumerate(data.groupby('features')): # Get colormap values = df.feature_values cmap, norm = scalar_palette(values=values, cmap='coolwarm', vmin=values.min(), vmax=values.max()) print(df) # Display fig, ax = plt.subplots() ax = sns.stripplot(x='timestep', y='shap_values', hue='feature_values', palette=cmap, data=df, ax=ax) # Needed for older matplotlib versions cmap = matplotlib.cm.get_cmap('coolwarm') # Configure axes plt.title(name) plt.legend([], [], frameon=False) ax.invert_xaxis() add_colorbar(plt.gcf(), cmap, norm) # End if int(i) > N: break # Show plt.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /_examples/shap/images/sphx_glr_plot_main05_009.png :alt: f0 :srcset: /_examples/shap/images/sphx_glr_plot_main05_009.png :class: sphx-glr-multi-img * .. image-sg:: /_examples/shap/images/sphx_glr_plot_main05_010.png :alt: f1 :srcset: /_examples/shap/images/sphx_glr_plot_main05_010.png :class: sphx-glr-multi-img * .. image-sg:: /_examples/shap/images/sphx_glr_plot_main05_011.png :alt: f2 :srcset: /_examples/shap/images/sphx_glr_plot_main05_011.png :class: sphx-glr-multi-img * .. image-sg:: /_examples/shap/images/sphx_glr_plot_main05_012.png :alt: f3 :srcset: /_examples/shap/images/sphx_glr_plot_main05_012.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none sample timestep features shap_values feature_values 0 0 0 f0 51 25 4 0 1 f0 28 91 8 0 2 f0 26 54 12 0 3 f0 80 93 16 0 4 f0 25 34 20 0 5 f0 58 89 24 1 0 f0 68 52 28 1 1 f0 94 24 32 1 2 f0 12 11 36 1 3 f0 84 36 40 1 4 f0 23 65 44 1 5 f0 65 40 48 2 0 f0 40 23 52 2 1 f0 44 7 56 2 2 f0 55 61 60 2 3 f0 91 96 64 2 4 f0 6 77 68 2 5 f0 90 18 72 3 0 f0 32 55 76 3 1 f0 86 78 80 3 2 f0 49 75 84 3 3 f0 91 48 88 3 4 f0 19 69 92 3 5 f0 82 1 96 4 0 f0 56 13 100 4 1 f0 50 93 104 4 2 f0 76 62 108 4 3 f0 56 8 112 4 4 f0 86 86 116 4 5 f0 86 5 120 5 0 f0 26 59 124 5 1 f0 97 49 128 5 2 f0 13 99 132 5 3 f0 9 14 136 5 4 f0 10 60 140 5 5 f0 96 85 144 6 0 f0 31 81 148 6 1 f0 38 89 152 6 2 f0 29 63 156 6 3 f0 87 32 160 6 4 f0 68 51 164 6 5 f0 56 68 168 7 0 f0 86 66 172 7 1 f0 67 20 176 7 2 f0 20 33 180 7 3 f0 53 61 184 7 4 f0 82 55 188 7 5 f0 16 97 192 8 0 f0 63 38 196 8 1 f0 63 47 200 8 2 f0 94 99 204 8 3 f0 89 50 208 8 4 f0 62 79 212 8 5 f0 1 41 216 9 0 f0 1 16 220 9 1 f0 87 71 224 9 2 f0 83 31 228 9 3 f0 45 86 232 9 4 f0 24 38 236 9 5 f0 57 7 sample timestep features shap_values feature_values 1 0 0 f1 39 44 5 0 1 f1 30 50 9 0 2 f1 60 73 13 0 3 f1 42 17 17 0 4 f1 40 60 21 0 5 f1 19 12 25 1 0 f1 92 9 29 1 1 f1 60 47 33 1 2 f1 13 53 37 1 3 f1 15 60 41 1 4 f1 34 48 45 1 5 f1 93 79 49 2 0 f1 38 6 53 2 1 f1 37 29 57 2 2 f1 58 27 61 2 3 f1 96 64 65 2 4 f1 77 95 69 2 5 f1 37 11 73 3 0 f1 39 36 77 3 1 f1 54 28 81 3 2 f1 86 41 85 3 3 f1 62 18 89 3 4 f1 43 33 93 3 5 f1 36 92 97 4 0 f1 44 63 101 4 1 f1 70 82 105 4 2 f1 78 48 109 4 3 f1 33 95 113 4 4 f1 71 49 117 4 5 f1 41 5 121 5 0 f1 81 46 125 5 1 f1 43 40 129 5 2 f1 50 45 133 5 3 f1 79 21 137 5 4 f1 49 56 141 5 5 f1 45 73 145 6 0 f1 44 96 149 6 1 f1 7 19 153 6 2 f1 44 49 157 6 3 f1 14 51 161 6 4 f1 82 25 165 6 5 f1 28 48 169 7 0 f1 89 51 173 7 1 f1 33 59 177 7 2 f1 24 2 181 7 3 f1 79 39 185 7 4 f1 65 42 189 7 5 f1 60 78 193 8 0 f1 81 44 197 8 1 f1 90 69 201 8 2 f1 88 79 205 8 3 f1 65 41 209 8 4 f1 96 71 213 8 5 f1 24 29 217 9 0 f1 78 12 221 9 1 f1 54 4 225 9 2 f1 81 7 229 9 3 f1 13 53 233 9 4 f1 81 87 237 9 5 f1 86 29 sample timestep features shap_values feature_values 2 0 0 f2 73 58 6 0 1 f2 32 29 10 0 2 f2 6 56 14 0 3 f2 59 45 18 0 4 f2 64 40 22 0 5 f2 84 55 26 1 0 f2 75 80 30 1 1 f2 29 5 34 1 2 f2 58 29 38 1 3 f2 80 57 42 1 4 f2 77 83 46 1 5 f2 60 65 50 2 0 f2 89 24 54 2 1 f2 5 91 58 2 2 f2 58 62 62 2 3 f2 16 43 66 2 4 f2 59 35 70 2 5 f2 59 19 74 3 0 f2 55 0 78 3 1 f2 55 49 82 3 2 f2 33 63 86 3 3 f2 94 52 90 3 4 f2 24 78 94 3 5 f2 93 29 98 4 0 f2 46 24 102 4 1 f2 98 50 106 4 2 f2 12 60 110 4 3 f2 44 35 114 4 4 f2 34 74 118 4 5 f2 40 37 122 5 0 f2 88 75 126 5 1 f2 65 3 130 5 2 f2 68 96 134 5 3 f2 14 59 138 5 4 f2 82 71 142 5 5 f2 75 83 146 6 0 f2 27 96 150 6 1 f2 53 51 154 6 2 f2 98 72 158 6 3 f2 34 17 162 6 4 f2 14 62 166 6 5 f2 96 41 170 7 0 f2 22 10 174 7 1 f2 64 8 178 7 2 f2 59 88 182 7 3 f2 37 95 186 7 4 f2 51 82 190 7 5 f2 15 19 194 8 0 f2 29 5 198 8 1 f2 41 68 202 8 2 f2 52 48 206 8 3 f2 1 31 210 8 4 f2 38 84 214 8 5 f2 54 69 218 9 0 f2 89 93 222 9 1 f2 94 78 226 9 2 f2 18 58 230 9 3 f2 48 74 234 9 4 f2 24 97 238 9 5 f2 91 89 sample timestep features shap_values feature_values 3 0 0 f3 41 52 7 0 1 f3 91 32 11 0 2 f3 25 30 15 0 3 f3 29 44 19 0 4 f3 6 58 23 0 5 f3 91 82 27 1 0 f3 12 58 31 1 1 f3 92 28 35 1 2 f3 18 25 39 1 3 f3 55 2 43 1 4 f3 23 71 47 1 5 f3 20 90 51 2 0 f3 45 95 55 2 1 f3 25 35 59 2 2 f3 30 75 63 2 3 f3 42 37 67 2 4 f3 24 89 71 2 5 f3 23 72 75 3 0 f3 93 49 79 3 1 f3 54 10 83 3 2 f3 58 36 87 3 3 f3 39 10 91 3 4 f3 10 25 95 3 5 f3 33 67 99 4 0 f3 85 64 103 4 1 f3 7 40 107 4 2 f3 49 27 111 4 3 f3 60 37 115 4 4 f3 86 68 119 4 5 f3 71 41 123 5 0 f3 15 60 127 5 1 f3 44 0 131 5 2 f3 53 53 135 5 3 f3 56 69 139 5 4 f3 45 94 143 5 5 f3 19 15 147 6 0 f3 70 41 151 6 1 f3 73 42 155 6 2 f3 91 87 159 6 3 f3 20 92 163 6 4 f3 40 54 167 6 5 f3 90 11 171 7 0 f3 92 77 175 7 1 f3 84 19 179 7 2 f3 76 14 183 7 3 f3 69 69 187 7 4 f3 97 99 191 7 5 f3 13 56 195 8 0 f3 12 41 199 8 1 f3 51 56 203 8 2 f3 85 14 207 8 3 f3 59 93 211 8 4 f3 74 62 215 8 5 f3 32 0 219 9 0 f3 32 81 223 9 1 f3 43 56 227 9 2 f3 57 31 231 9 3 f3 6 46 235 9 4 f3 87 31 239 9 5 f3 40 98 .. GENERATED FROM PYTHON SOURCE LINES 308-317 Display using ``sns.swarmplot`` ------------------------------- .. note: If the number of samples is too high, the points overlap and are ignored by the ``swarmplot`` library. In such scenario it is better to use ``stripplot``. Let's display the shap values for each timestep. .. GENERATED FROM PYTHON SOURCE LINES 317-394 .. code-block:: default :lineno-start: 318 # Loop for i, (name, df) in enumerate(data.groupby('features')): # Get colormap values = df.feature_values cmap, norm = scalar_palette(values=values, cmap='coolwarm', vmin=values.min(), vmax=values.max()) # Display fig, ax = plt.subplots() ax = sns.swarmplot(x='timestep', y='shap_values', hue='feature_values', palette=cmap, data=df, size=2, ax=ax) # Needed for older matplotlib versions cmap = matplotlib.cm.get_cmap('coolwarm') # Configure axes plt.title(name) plt.legend([], [], frameon=False) ax.invert_xaxis() add_colorbar(plt.gcf(), cmap, norm) # End if int(i) > N: break # Show plt.show() """ sns.set_theme(style="ticks") # Create a dataset with many short random walks rs = np.random.RandomState(4) pos = rs.randint(-1, 2, (20, 5)).cumsum(axis=1) pos -= pos[:, 0, np.newaxis] step = np.tile(range(5), 20) walk = np.repeat(range(20), 5) df = pd.DataFrame(np.c_[pos.flat, step, walk], columns=["position", "step", "walk"]) # Initialize a grid of plots with an Axes for each walk #grid = sns.FacetGrid(df_stack, col="walk", hue="f", palette="tab20c", # col_wrap=4, height=1.5) grid = sns.FacetGrid(df_stack, hue="f", palette="tab20c", height=1.5) # Draw a horizontal line to show the starting point grid.refline(y=0, linestyle=":") # Draw a line plot to show the trajectory of each random walk grid.map(plt.plot, "t", "value", marker="o") # Adjust the tick positions and labels grid.set(xticks=np.arange(5), yticks=[-3, 3], xlim=(-.5, 4.5), ylim=(-3.5, 3.5)) # Adjust the arrangement of the plots grid.fig.tight_layout(w_pad=1) """ #plt.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /_examples/shap/images/sphx_glr_plot_main05_013.png :alt: f0 :srcset: /_examples/shap/images/sphx_glr_plot_main05_013.png :class: sphx-glr-multi-img * .. image-sg:: /_examples/shap/images/sphx_glr_plot_main05_014.png :alt: f1 :srcset: /_examples/shap/images/sphx_glr_plot_main05_014.png :class: sphx-glr-multi-img * .. image-sg:: /_examples/shap/images/sphx_glr_plot_main05_015.png :alt: f2 :srcset: /_examples/shap/images/sphx_glr_plot_main05_015.png :class: sphx-glr-multi-img * .. image-sg:: /_examples/shap/images/sphx_glr_plot_main05_016.png :alt: f3 :srcset: /_examples/shap/images/sphx_glr_plot_main05_016.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none '\nsns.set_theme(style="ticks")\n\n# Create a dataset with many short random walks\nrs = np.random.RandomState(4)\npos = rs.randint(-1, 2, (20, 5)).cumsum(axis=1)\npos -= pos[:, 0, np.newaxis]\nstep = np.tile(range(5), 20)\nwalk = np.repeat(range(20), 5)\ndf = pd.DataFrame(np.c_[pos.flat, step, walk],\n columns=["position", "step", "walk"])\n# Initialize a grid of plots with an Axes for each walk\n#grid = sns.FacetGrid(df_stack, col="walk", hue="f", palette="tab20c",\n# col_wrap=4, height=1.5)\n\ngrid = sns.FacetGrid(df_stack, hue="f",\n palette="tab20c", height=1.5)\n\n# Draw a horizontal line to show the starting point\ngrid.refline(y=0, linestyle=":")\n\n# Draw a line plot to show the trajectory of each random walk\ngrid.map(plt.plot, "t", "value", marker="o")\n\n# Adjust the tick positions and labels\ngrid.set(xticks=np.arange(5), yticks=[-3, 3],\n xlim=(-.5, 4.5), ylim=(-3.5, 3.5))\n\n# Adjust the arrangement of the plots\ngrid.fig.tight_layout(w_pad=1)\n\n' .. GENERATED FROM PYTHON SOURCE LINES 395-398 Display using ``sns.FacetGrid`` ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 398-404 .. code-block:: default :lineno-start: 399 #g = sns.FacetGrid(df_stack, col="f", hue='original') #g.map(sns.swarmplot, "t", "value", alpha=.7) #g.add_legend() .. GENERATED FROM PYTHON SOURCE LINES 405-408 Display using ``shap.beeswarm`` ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 408-413 .. code-block:: default :lineno-start: 409 # REF: https://github.com/slundberg/shap/blob/master/shap/plots/_beeswarm.py # # .. note: It needs a kernel explainer, and while it works with # common kernels (plot_main07.py) it does not work with # the DeepKernel for some reason (mask related). .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 4.630 seconds) .. _sphx_glr_download__examples_shap_plot_main05.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_main05.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_main05.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_