Note
Click here to download the full example code
07.b stats.2dbin
and sns.heatmap
Use binned_statistic_2d and display using heatmap.
9 import numpy as np
10 import pandas as pd
11 import seaborn as sns
12 import matplotlib.pyplot as plt
13
14 from pathlib import Path
15 from scipy import stats
16
17 # Load data
18 path = Path('../../datasets/shap/')
19 data = pd.read_csv(path / 'shap.csv')
20
21 # Get x, y and z
22 x = data.timestep
23 y = data.shap_values
24 z = data.feature_values
25
26 # Show
27 data[['timestep', 'shap_values', 'feature_values']]
28
29 # Binned stats
30 statistic, x_edge, y_edge, binnumber = \
31 stats.binned_statistic_2d(x=x, y=y, values=z,
32 statistic='count', bins=[20, x.nunique()],
33 expand_binnumbers=False)
34
35 # Display
36 sns.heatmap(statistic, annot=True, linewidth=.5,
37 cmap='coolwarm', annot_kws={"fontsize":6},
38 square=False)
39
40 # Show
41 plt.show()
Total running time of the script: ( 0 minutes 0.430 seconds)