我试图列出使用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()
还有我的Lime代码:
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
答案 0 :(得分:1)
您应使用 lime_tabular.RecurrentTabularExplainer 而不是 lime_tabular.LimeTabularExplainer 。因为它是keras式递归神经网络的解释器。 查看LIME软件包中提供的教程。