marcotcr / lime

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

IndexError: index 1 is out of bounds for axis 1 with size 1 - TextClassification 0 or 1 #437

Closed raed19 closed 4 years ago

raed19 commented 4 years ago

I am using LIME to explain binary classification 0 or 1 as follows : `from keras.preprocessing.text import Tokenizer from keras.preprocessing import sequence

def new_predict(texts): seq = token.texts_to_sequences(texts) text_data = sequence.pad_sequences( seq, maxlen= 60)

it has to be predict the whole string not single one

return model.predict_proba(text_data)

This here for Lime :

UNI_labels = data['Label']

just zero and 1 which is class name, that is it

class_names= np.array(list(UNI_labels.unique())) print(class_names) class_names explainer = LimeTextExplainer(class_names=class_names)

x_test_data = np.array(x_test_data) y_test_data = np.array(y_test_data) idx = 550 exp = explainer.explain_instance(x_test_data[idx], new_predict , num_features=3, labels = [0,1] ) print('Document id: %d' % idx)

print('Predicted class =', class_names[pre])

print('True class: %s' % class_names[y_test_data[idx]])`

but I got this error : IndexError: index 1 is out of bounds for axis 1 with size 1

I tried another thins which is modified this line exp = explainer.explain_instance(x_test_data[idx], new_predict ,top_labels = 2, num_features=3, labels = [0,1] ) it will work, but it will give me an error always when I use the following with label 1 print ('\n'.join(map(str, exp.as_list(label = 1 )))) The error is : KeyError: 1

if I used label 0, it will work

Could you please help me with that ?

I appreciate your effort and time

Thanks in advance

marcotcr commented 4 years ago

See #428, I'm guessing your predict function is not returning a 2d array.