我建立了一个深度神经网络来分类欺诈性交易。我试图用LIME来解释,但是遇到了来自interpretor.explain_instance()函数的错误。
完整的代码如下:
import lime
from lime import lime_tabular
interpretor = lime_tabular.LimeTabularExplainer(
training_data=x_train_scaled,
feature_names=X_train.columns,
mode='classification'
)
exp = interpretor.explain_instance(
data_row=x_test_scaled[:1], ##new data
predict_fn=model.predict,num_features=11
)
xp.show_in_notebook(show_table=True)这会引发错误:
--
IndexError Traceback (most recent call last)
/tmp/ipykernel_33/1730959582.py in <module>
1 exp = interpretor.explain_instance(
2 data_row=x_test_scaled[1], ##new data
----> 3 predict_fn=model.predict
4 )
5
/opt/conda/lib/python3.7/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)
457 num_features,
458 model_regressor=model_regressor,
--> 459 feature_selection=self.feature_selection)
460
461 if self.mode == "regression":
/opt/conda/lib/python3.7/site-packages/lime/lime_base.py in explain_instance_with_data(self, neighborhood_data, neighborhood_labels, distances, label, num_features, feature_selection, model_regressor)
180
181 weights = self.kernel_fn(distances)
--> 182 labels_column = neighborhood_labels[:, label]
183 used_features = self.feature_selection(neighborhood_data,
184 labels_column,
IndexError: index 1 is out of bounds for axis 1 with size 1发布于 2022-10-26 20:10:29
在labels=(0,)中添加exp = eplainer.explain_instance()可以解决您的问题。
exp = interpretor.explain_instance(
data_row=x_test_scaled[:1], ##new data
predict_fn=model.predict,
num_features=11,
labels=(0,)
)我也有一个类似的问题,乳腺癌数据试图预测良性肿瘤或恶性肿瘤。包含记录的示例的列标题为benign_0_malignant_1,每行中放置0或1。
发布于 2022-06-08 17:32:15
我认为问题在于您传递的是一个2D数组,但是根据医生的说法 explain_instance()希望实例是一个一维数组。
请注意,来自2D数组的单行片本身就是2D:
>>> import numpy as np
>>> arr = np.array([[1, 2], [3, 4], [5, 6]])
>>> arr[:1]
array([[1, 2]])另一个问题是预测函数,它将产生一系列概率,而不是单个预测。再一次,医生们解释(我的重点):
classifier_fn-分类器预测概率函数,该函数采用numpy数组并输出预测概率.对于ScikitClassifiers,这是classifier.predict_proba。
要解决这些问题,请使用一个简单的索引,而不是切片,并将x_test_scaled作为预测函数传递给model.predict_proba:
exp = interpretor.explain_instance(data_row=x_test_scaled[0],
predict_fn=model.predict_proba,
num_features=11
)https://stackoverflow.com/questions/72542202
复制相似问题