marcotcr / lime

Lime: Explaining the predictions of any machine learning classifier
BSD 2-Clause "Simplified" License
11.55k stars 1.8k forks source link

Small fix for error when labels are non-integer #627

Closed imiordanov closed 3 years ago

imiordanov commented 3 years ago

Hey there,

First of all, let me thank you for your work! Truly great tool!

I was playing around and I wanted to get results from LIME about a binary image classification model, and I stumbled across a small hiccup. When I would set the labels parameter to an array of strings, for example ["OK", "NG"], I would get an error from lime_base.py on line 182 that indices need to be integers or ellipses. The function I was calling is LimeImageExplainer.explain_instance, so I figured there was something wrong there. The label is passed as it is, so naturally "OK" is not a valid index. I've introduced a simple enumeration over the labels, so I think the rest of the code should work as intended. If I have missed anything, please let me know.

Thank you again!

Cheers.

marcotcr commented 3 years ago

Hi there. I don't think this is a bug, we do expect labels to contain indices rather than label names. Your code will always generate explanations for labels [0, 1, 2...] rather than the top labels actually predicted (notice how enumerate will always give us this sequence regardless of what top_labels is)

imiordanov commented 3 years ago

I see, actually this gives me some better insight into the code! Thank you, I will take this into consideration!