Search code examples
pythonxgboostshap

Why are shap values changing every time I call shap.plots.beeswarm?


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.


Solution

  • 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)