0

I want to draw shap partial dependence plots with regression lines + and histograms.

Representing SHAP partial dependence plots (scatter plot and a regression line represented with line and shade) + histogram on right and top are distribution of the SHAP and values of variables.

Reference Article : https://www.nature.com/articles/s41598-021-99920-7

Here are a visualize graphs. Regards : Junaid [enter image description here](https://i.stack.imgur.com/bfbCJ.jpg)

I tried how to use ax=ax in SHAP scatter plots. But i am unable to draw these kind of graphs.

1 Answers1

0

This works for me:

import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import shap
import numpy as np

fig = plt.figure(figsize=(12, 12))

gs = gridspec.GridSpec(5, 5)
ax_main = plt.subplot(gs[1:5, :4])
ax_xDist = plt.subplot(gs[0, :4])
ax_yDist = plt.subplot(gs[1:5, 4])
  
# lowess
idx = np.where(X.columns==var_one)[0][0]
x = X.iloc[:,idx]
y_sv = shap_values[:,idx]
lowess = sm.nonparametric.lowess(y_sv, x, frac=.3)
ax_main.plot(*list(zip(*lowess)), color="#312D2C", linestyle="dashed", )

# shap
shap.dependence_plot(var_one,
                     shap_values,
                     X,
                     interaction_index=var_two,
                     alpha=0.5,
                     dot_size=10,
                     show=False,
                     ax=ax_main)

# histplots
ax_xDist.hist(X[var_one], bins=50, edgecolor="black", color="gray")
ax_yDist.hist(X[var_two], orientation="horizontal", bins=50, edgecolor="black", color="gray")

plt.show()
Marina W.
  • 91
  • 2
  • 10
  • But, unfortunately i can't edit font size. How can I edit my code to change font size? – Junaid Latif Feb 09 '23 at 08:33
  • The specific code changes depend on what font you want to edit (title, labels, etc.), but you can edit the font size just like you would typically with matplotlib (see [this](https://stackoverflow.com/questions/12444716/how-do-i-set-the-figure-title-and-axes-labels-font-size) related issue). – Marina W. Feb 10 '23 at 16:12
  • I solved it. Thanks... I amend my code regarding tonmy needs. – Junaid Latif Feb 11 '23 at 17:14