shap / shap

A game theoretic approach to explain the output of any machine learning model.
https://shap.readthedocs.io
MIT License
22.9k stars 3.29k forks source link

BUG: Divide by zero #3248

Open mfedenia opened 1 year ago

mfedenia commented 1 year ago

Issue Description

I ran into a divide by zero issue in _violin.py when nbins =1

I'm not sure when this situation occurs but the module halts and does not give enough information to resolve it with the data it is receiving or assign a color that does not compromise the output.

Minimal Reproducible Example

easy fix

#Edge case
                # c = pl.get_cmap(color)(i / (
                #         nbins - 1)) if color in pl.cm.datad else color  # if color is a cmap, use it, otherwise use a color
                if nbins == 1:
                    c = pl.get_cmap(color)(0) if color in pl.cm.datad else color  # use the start of the color map or the single color
                else:
                    c = pl.get_cmap(color)(i / (nbins - 1)) if color in pl.cm.datad else color  # if color is a cmap, use it, otherwise use a color

not sure if this is best

Traceback

Traceback (most recent call last):
  File "/fnr/bart_py/./src/shap-gpu.py", line 266, in <module>
    shap.plots.violin(
  File "/home/m/.local/lib/python3.10/site-packages/shap-0.42.1-py3.10-linux-x86_64.egg/shap/plots/_violin.py", line 299, in violin
    c = pl.get_cmap(color)(i / (
ZeroDivisionError: division by zero

Expected Behavior

No response

Bug report checklist

Installed Versions

shap==0.42.1

znacer commented 1 year ago

Thanks for raising this issue. Could you provide a more complete reproducible example ? Containing everything from imports to the line of the error, using data from shap.datasets or randomly generated data.