有人可以帮我理解输出LIME
吗?https://github.com/marcotcr/lime(但是,我想使用keras
而不是sklearn
。)
所以我有一个简单的二元情感分类任务(类标签 0 和 1)。我训练一个keras
网络并LimeTextExplainer
在一个随机文档(document[idx]
)上运行。由于keras
没有predict_proba
方法但LimeTextExplainer
需要此功能,我创建了自己的:
def predict_prob(string):
''' must take list of d strings and output (d,k) numpy array
with prediction probabilities, where k is the number of classes
'''
x_temp = count.transform(np.array(string)) ## transform string
prediction = model.predict(convert_sparse_matrix_to_sparse_tensor(x_temp))
class_zero = 1-prediction
probability= np.append(class_zero, prediction, axis=1)
return probability ## array [1-p, p]
我理解文本解释器的输出如下:负值单词的出现更可能导致第 0 类,而正值导致第 1 类。
from lime.lime_text import LimeTextExplainer
from lime import lime_text
explainer = LimeTextExplainer(class_names=['negative','positive'])
ex = explainer.explain_instance(document[idx], predict_prob,
num_features=10)
from collections import OrderedDict
weights = OrderedDict(ex.as_list())
lime_weights = pd.DataFrame({'words': list(weights.keys()), 'weights': list(weights.values())})
但是,我不太明白的是SubmodularPick
.
from lime import submodular_pick
sp_obj = submodular_pick.SubmodularPick(explainer, document, predict_prob, sample_size=10, num_features=feature_n, num_exps_desired=2)
如何(全局)检索最相关的特征?是sp_obj.explanations
还是sp_obj.sp_explanations
??有什么区别?
W_matrix = pd.DataFrame([dict(this.as_list()) for this in sp_obj.explanations])
在错误中为我运行结果...
File "C:\Users\Anaconda3\envs\pyGPU\lib\site-packages\lime\explanation.py", line 141, in as_list
ans = self.domain_mapper.map_exp_ids(self.local_exp[label_to_use], **kwargs)
KeyError: 1
另一方面,sp_obj.sp_explanations
作品:
W_pick=pd.DataFrame([dict(this.as_list(this.available_labels()[0])) for this in sp_obj.sp_explanations]).fillna(0)
以下真的是全球负类和正类的输出吗?
# negative class
[ex.as_pyplot_figure(label=0) for ex in sp_obj.sp_explanations]
# positive class
[ex.as_pyplot_figure(label=1) for exp in sp_obj.sp_explanations]
再次在这里:负类的第一个函数导致ans = self.domain_mapper.map_exp_ids(self.local_exp[label_to_use], **kwargs) KeyError: 0
错误,但图片显示为任一方式..
负/正值是什么意思?