2

I have a numeric health record dataset. I used a 1D CNN keras model for the classification step.

I am giving a reproductible example in Python:

import tensorflow as tf
import keras
from keras.models import Sequential
from keras.layers import Conv1D, Activation, Flatten, Dense
import numpy as np

a = np.array([[0,1,2,9,3], [0,5,1,33,6], [1, 12,1,8,9]])
train = np.reshape(a[:,1:],(a[:,1:].shape[0], a[:,1:].shape[1],1))
y_train = keras.utils.to_categorical(a[:,:1])

model = Sequential()
model.add(Conv1D(filters=2, kernel_size=2, strides=1, activation='relu', padding="same", input_shape=(train.shape[1], 1), kernel_initializer='he_normal'))

model.add(Flatten())
model.add(Dense(2, activation='sigmoid'))

model.compile(loss=keras.losses.binary_crossentropy,
                 optimizer=keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False),
                 metrics=['accuracy'])

model.fit(train, y_train, epochs=3, verbose=1)

I am getting this error when I apply lime to my 1D CNN model

IndexError: boolean index did not match indexed array along dimension 1; dimension is 4 but corresponding boolean dimension is 1
import lime
import lime.lime_tabular

explainer = lime.lime_tabular.LimeTabularExplainer(train)

Is there a solution ?

Noura
  • 474
  • 2
  • 11

2 Answers2

1

I did some minor changes to your initial code (changed from keras to tensorflow.keras)

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv1D, Activation, Flatten, Dense
import numpy as np

a = np.array([[0,1,2,9,3], [0,5,1,33,6], [1, 12,1,8,9]])
train = np.reshape(a[:,1:],(a[:,1:].shape[0], a[:,1:].shape[1],1))
y_train = tf.keras.utils.to_categorical(a[:,:1])

model = Sequential()
model.add(Conv1D(filters=2, kernel_size=2, strides=1, activation='relu', 
     padding="same", input_shape=(train.shape[1], 1), 
kernel_initializer='he_normal'))

model.add(Flatten())
model.add(Dense(2, activation='sigmoid'))

model.compile(loss=tf.keras.losses.binary_crossentropy,
             optimizer=tf.keras.optimizers.Adam(lr=0.001, beta_1=0.9, 
             beta_2=0.999, amsgrad=False),
             metrics=['accuracy'])

model.fit(train, y_train, epochs=3, verbose=1)

Then I added some test data because you don't want to train and test your LIME model on the same data

b = np.array([[1,4,5,3,2], [1,4,2,55,1], [7, 3,22,3,10]])
test = np.reshape(b[:,1:],(b[:,1:].shape[0], b[:,1:].shape[1],1))

Here I show how the RecurrentTabularExplainer can be trained

import lime
from lime import lime_tabular

explainer = lime_tabular.RecurrentTabularExplainer(train,training_labels=y_train, feature_names=["random clf"],
                                               discretize_continuous=False, feature_selection='auto', class_names=['class 1','class 2'])

Then you can run your LIME model on one of the examples in your test data:

exp = explainer.explain_instance(np.expand_dims(test[0],axis=0), model.predict, num_features=10)

and finally display the predictions

exp.show_in_notebook()

explanations from LIME

or just printing the prediction

print(exp.as_list())
bjornsing
  • 322
  • 6
  • 25
0

You should try lime_tabular.RecurrentTabularExplainer instead of LimeTabularExplainer. It is an explainer for keras-style recurrent neural networks. Check out the examples in LIME documentation for better understanding. Good luck:)

atinjanki
  • 483
  • 3
  • 13
  • It didn't work lime.lime_tabular.RecurrentTabularExplainer(train, feature_names=None) Traceback (most recent call last): File "", line 1, in File "/lib/python3.6/site-packages/lime/lime_tabular.py", line 624, in __init__ for n in feature_names for i in range(n_timesteps)] TypeError: 'NoneType' object is not iterable – Noura Feb 06 '20 at 16:26
  • _TypeError: 'NoneType' object is not iterable_ appears to be a python error. Do check what is causing it – atinjanki Feb 06 '20 at 20:50
  • I can't figure out the problem – Noura Feb 07 '20 at 15:25
  • TypeError: 'NoneType' object is not iterable - means the object causing this error has value 'none' , which means that object does not contain any value. Check which object is causing it in your code. – atinjanki Feb 11 '20 at 23:11
  • This is why I gave a reproductioble example because I'm still having the same error. lime.lime_tabular.RecurrentTabularExplainer(train, training_labels=y_train , feature_names=None) File "lime/lime_tabular.py", line 624, in __init__ for n in feature_names for i in range(n_timesteps)] TypeError: 'NoneType' object is not iterable – Noura Feb 12 '20 at 15:44
  • @Noura Did you find what was the problem? – Alexandros Evangelou Aug 31 '20 at 12:36