marcotcr / lime

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

Is there a way to change the weights of already existing explanation? #581

Closed Rocyzas closed 3 years ago

Rocyzas commented 3 years ago

Hello, I am using LimeTabularExplainer on the classification task. It predicts classes and numerates important features correctly. However, it seems to fail to explain each instance correctly, by always pointing feature 'bars' towards the wrong class (even though it predicts and ranks important features correctly). I am using an SVC classifier from the scikit-learn library on the synthetic dataset, where '1' and '3' are the most important features.

I checked both the output and explanation.as_list(label) and it seems that the explanation 'bars' should be reversed for all predictions it makes (or at least it seems to be about right if I would reverse them). If that is the case, perhaps it is possible to multiply each weighted feature by -1, so that it rotates? If that is not possible or I should not do that, could you advise how am I supposed to fix this?


    # X - training data without labels
    # y: [0,1]
    # Two class names AD and HC
    # Columns: ['0', '1', '2', '3']
    explainer = LimeTabularExplainer(X, mode = 'classification',
                                        training_labels=y,
                                        feature_selection= 'auto',
                                        class_names=class_names,
                                        feature_names = columns,
                                        discretize_continuous=False)

 # rows - rows that need to be explained
    for i in range(len(rows)):
        explanation = explainer.explain_instance(rows[i],
                                    clf.predict_proba,
                                    top_labels=1, # top_labels=1
                                    num_features=len(columns))
        html_data = explanation.as_html()
        HTML(data=html_data)

HCAD3 HCAD2 HCAD1 FISVC

Versions: python=3.7.5 lime=0.2.0.1

I am new to the lime library, so I might have made a silly mistake. Sorry if that's the case.

marcotcr commented 3 years ago

I think there is a bug if you call top_labels=1 when there are only two labels. When you have binary classification, I suggest just not using that argument. Please comment if this doesn't solve your issue.