7

So here's my code using shap :

enter image description here

Since I just plot three times the same shape values, I'd expect the three plots to be the same. However, it keeps on changing. After some research, it seems that a new value appear at the top at each call, but why ? Is it a bug in shap ?

Edit 1 : I tried loading the same model between each call of shap.plots.beeswarm, but the results are still different.

ah bon
  • 9,293
  • 12
  • 65
  • 148
SashimiDélicieux
  • 476
  • 1
  • 5
  • 17

2 Answers2

8

Thanks to @jared_mamrot I could find a solution. I just had to change the code of shap's function beeswarm :

Line 57 : shap_exp = shap_values

replaced by :

Line 57 : shap_exp = copy.deepcopy(shap_values)

Make sure to import the library at the top of the code :

import copy

Edit : If you don't want to change the source code, just give a deep copy of shap_values while calling beeswarm like so :

import copy    
shap.plots.beeswarm(copy.deepcopy(shap_values), max_display=15)
SashimiDélicieux
  • 476
  • 1
  • 5
  • 17
3

Based on this github issue the problem relates to the creation of the "Sum of xx other features" variable. This variable is meant to be temporary i.e. you only use it once for plotting (if you run shap.plots.beeswarm again the results are wrong). So I guess a solution to your problem is "don't run it multiple times and there is no problem".

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46