首页 > 解决方案 > 将石灰表格解释器与 Keras 一起使用时出现关键错误

问题描述

我正在尝试使用 Lime 列出 Keras 神经网络回归模型的特征重要性。

我已经尝试了许多不同的代码变体,并不断得到一些版本的 KeyError: 4 ,其中数字不同。我尝试过更改目标标签和功能数量,以及是否将数据帧转换为数组。

我的模型:

model = Sequential()
model.add(Dense(units=1, input_dim=6, activation='relu'))
model.add(LeakyReLU(alpha=.001))
model.add(Dense(256, activation='relu'))
model.add(LeakyReLU(alpha=.001))
model.add(Dense(units=1, input_dim=3, activation='relu'))
model.add(LeakyReLU(alpha=.001))
model.add(Dense(64, activation='relu'))
model.add(LeakyReLU(alpha=.001))
model.add(Dense(32, activation='relu'))
model.add(LeakyReLU(alpha=.001))
model.add(Dense(units=1, input_dim=1, activation='relu'))
model.summary()

还有我的石灰代码:

import lime
import lime.lime_tabular
# declare lime explainer
explainer = lime.lime_tabular.LimeTabularExplainer(x_train.values, feature_names=list(x_train.columns),
                                                   verbose=True)
# declare explainer and run
exp = explainer.explain_instance(y_train['absorb_pct'], model.predict(x_train), 
                                 num_features=len(list(x_train.columns)))
exp.show_in_notebook(show_table=True)

x_train 包括除吸收 pct 之外的所有特征,吸收 pct 是我的目标变量

我想获得一些表格数据,如本页所示:https ://pythondata.com/local-interpretable-model-agnostic-explanations-lime-python/

但不断得到:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-32-8d39e1aec03b> in <module>
      6 # declare explainer and run
      7 exp = explainer.explain_instance(y_train['absorb_pct'], model.predict(x_train), 
----> 8                                  num_features=len(list(x_train.columns)))
      9 exp.show_in_notebook(show_table=True)

/anaconda3/lib/python3.6/site-packages/lime/lime_tabular.py in explain_instance(self, data_row, predict_fn, labels, top_labels, num_features, num_samples, distance_metric, model_regressor)
    309             explanations.
    310         """
--> 311         data, inverse = self.__data_inverse(data_row, num_samples)
    312         scaled_data = (data - self.scaler.mean_) / self.scaler.scale_
    313 

/anaconda3/lib/python3.6/site-packages/lime/lime_tabular.py in __data_inverse(self, data_row, num_samples)
    462             first_row = data_row
    463         else:
--> 464             first_row = self.discretizer.discretize(data_row)
    465         data[0] = data_row.copy()
    466         inverse = data.copy()

/anaconda3/lib/python3.6/site-packages/lime/discretize.py in discretize(self, data)
    107         for feature in self.lambdas:
    108             if len(data.shape) == 1:
--> 109                 ret[feature] = int(self.lambdas[feature](ret[feature]))
    110             else:
    111                 ret[:, feature] = self.lambdas[feature](

/anaconda3/lib/python3.6/site-packages/pandas/core/series.py in __getitem__(self, key)
    866         key = com.apply_if_callable(key, self)
    867         try:
--> 868             result = self.index.get_value(self, key)
    869 
    870             if not is_scalar(result):

/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   4373         try:
   4374             return self._engine.get_value(s, k,
-> 4375                                           tz=getattr(series.dtype, 'tz', None))
   4376         except KeyError as e1:
   4377             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 4

标签: python-3.xkeraslime

解决方案


您应该使用lime_tabular.RecurrentTabularExplainer而不是lime_tabular.LimeTabularExplainer。因为它是 keras 风格的递归神经网络的解释器。检查 LIME 包中提供的教程。


推荐阅读