Note
Click here to download the full example code
07. Basic example
Out:
16%|=== | 5351/32561 [00:11<00:55]
18%|==== | 5863/32561 [00:12<00:54]
20%|==== | 6387/32561 [00:13<00:53]
21%|==== | 6910/32561 [00:14<00:51]
23%|===== | 7434/32561 [00:15<00:50]
24%|===== | 7960/32561 [00:16<00:49]
26%|===== | 8477/32561 [00:17<00:48]
28%|====== | 8997/32561 [00:18<00:47]
29%|====== | 9519/32561 [00:19<00:45]
31%|====== | 10038/32561 [00:20<00:44]
32%|====== | 10550/32561 [00:21<00:43]
34%|======= | 11057/32561 [00:22<00:42]
36%|======= | 11569/32561 [00:23<00:41]
37%|======= | 12097/32561 [00:24<00:40]
39%|======== | 12616/32561 [00:25<00:39]
40%|======== | 13140/32561 [00:26<00:38]
42%|======== | 13665/32561 [00:27<00:37]
44%|========= | 14187/32561 [00:28<00:36]
45%|========= | 14704/32561 [00:29<00:35]
47%|========= | 15220/32561 [00:30<00:34]
48%|========== | 15741/32561 [00:31<00:33]
50%|========== | 16250/32561 [00:32<00:32]
51%|========== | 16762/32561 [00:33<00:31]
53%|=========== | 17281/32561 [00:34<00:30]
55%|=========== | 17803/32561 [00:35<00:29]
56%|=========== | 18325/32561 [00:36<00:27]
58%|============ | 18846/32561 [00:37<00:26]
59%|============ | 19365/32561 [00:38<00:25]
61%|============ | 19883/32561 [00:39<00:24]
63%|============= | 20404/32561 [00:40<00:23]
64%|============= | 20922/32561 [00:41<00:22]
66%|============= | 21438/32561 [00:42<00:21]
67%|============= | 21937/32561 [00:43<00:20]
69%|============== | 22451/32561 [00:44<00:19]
71%|============== | 22969/32561 [00:45<00:18]
72%|============== | 23491/32561 [00:46<00:17]
74%|=============== | 24017/32561 [00:47<00:16]
75%|=============== | 24538/32561 [00:48<00:15]
77%|=============== | 25062/32561 [00:49<00:14]
79%|================ | 25567/32561 [00:50<00:13]
80%|================ | 26077/32561 [00:51<00:12]
82%|================ | 26563/32561 [00:52<00:11]
83%|================= | 27073/32561 [00:53<00:10]
85%|================= | 27575/32561 [00:54<00:09]
86%|================= | 28071/32561 [00:55<00:08]
88%|================== | 28591/32561 [00:56<00:07]
89%|================== | 29105/32561 [00:57<00:06]
91%|================== | 29628/32561 [00:58<00:05]
93%|=================== | 30150/32561 [00:59<00:04]
94%|=================== | 30676/32561 [01:00<00:03]
96%|=================== | 31188/32561 [01:01<00:02]
97%|=================== | 31705/32561 [01:02<00:01]
99%|===================| 32224/32561 [01:03<00:00]
6 # Libraries
7 import xgboost
8 import shap
9 import matplotlib.pyplot as plt
10
11 # Load shap dataset
12 X, y = shap.datasets.adult()
13
14 # Train model
15 model = xgboost.XGBClassifier().fit(X, y)
16
17 # Create shap explainer
18 explainer = shap.Explainer(model, X)
19 shap_values = explainer(X)
20
21
22 # Create beeswarm plot using explainer
23 shap.plots.beeswarm(shap_values,
24 max_display=12,
25 order=shap.Explanation.abs.mean(0))
26
27 # Adjust
28 plt.tight_layout()
Total running time of the script: ( 1 minutes 6.443 seconds)