gpt4 book ai didi

python - Lime vs TreeInterpreter 用于解释决策树

转载 作者:IT老高 更新时间:2023-10-28 20:48:25 38 4
gpt4 key购买 nike

石灰来源:https://github.com/marcotcr/lime

树解释器来源:tree interpreter

我试图了解 DecisionTree 如何使用 Lime 和 treeinterpreter 进行预测。虽然两者都声称他们能够在他们的描述中解释决策树。似乎两者都以不同的方式解释相同的 DecisionTree。即特征贡献order。这怎么可能?如果两者都在看同一件事,并试图描述同一事件,但按不同顺序分配重要性。

我们应该信任谁?尤其是最重要的特征在预测中很重要的地方。

树的代码

import sklearn
import sklearn.datasets
import sklearn.ensemble
import numpy as np
import lime
import lime.lime_tabular
from __future__ import print_function
np.random.seed(1)
from treeinterpreter import treeinterpreter as ti
from sklearn.tree import DecisionTreeClassifier

iris = sklearn.datasets.load_iris()

dt = DecisionTreeClassifier(random_state=42)
dt.fit(iris.data, iris.target)

n = 100


instances =iris.data[n].reshape(1,-1)

prediction, biases, contributions = ti.predict(dt, instances)


for i in range(len(instances)):
print ("prediction:",prediction)
print ("-"*20)
print ("Feature contributions:")
print ("-"*20)

for c, feature in sorted(zip(contributions[i],
iris.feature_names),
key=lambda x: ~abs(x[0].any())):

print (feature, c)

石灰的代码

import sklearn
import sklearn.datasets
import sklearn.ensemble
import numpy as np
import lime
import lime.lime_tabular
from __future__ import print_function
np.random.seed(1)
from sklearn.tree import DecisionTreeClassifier

iris = sklearn.datasets.load_iris()

dt = DecisionTreeClassifier(random_state=42)
dt.fit(iris.data, iris.target)

explainer = lime.lime_tabular.LimeTabularExplainer(iris.data, feature_names=iris.feature_names,
class_names=iris.target_names,
discretize_continuous=False)

n = 100


exp = explainer.explain_instance(iris.data[n], dt.predict_proba, num_features=4, top_labels=2)

exp.show_in_notebook(show_table=True, predict_proba= True , show_predicted_value = True , show_all=False)

让我们先看看的输出。

enter image description here

所以 a 它确实正确地说它是 virginica。但是,通过在

中分配重要性

1) 花瓣宽度 (cm) 然后花瓣长度 (cm)

现在让我们看看 lime

的输出

enter image description here

是的,它确实说算法预测了 virginica,但是看看它是如何进行分类的,我们清楚地看到以下内容

1)花瓣长度(cm)>花瓣宽度(cm)以石灰代替花瓣长度(cm)<花瓣宽度(cm)如图所示

2) 其中萼片宽度和萼片长度预测为零,石灰声称具有一定的值(value),如上传的图片所示

这里发生了什么?

当特征达到 1000+ 时,问题就越来越大,每个数字都对做出决定很重要。

最佳答案

为什么这两种方法可能产生不同的结果?

Lime:对其工作原理的简短说明,摘自他们的github page :

Intuitively, an explanation is a local linear approximation of the model's behaviour. While the model may be very complex globally, it is easier to approximate it around the vicinity of a particular instance. While treating the model as a black box, we perturb the instance we want to explain and learn a sparse linear model around it, as an explanation. The figure below illustrates the intuition for this procedure. The model's decision function is represented by the blue/pink background, and is clearly nonlinear. The bright red cross is the instance being explained (let's call it X). We sample instances around X, and weight them according to their proximity to X (weight here is indicated by size). We then learn a linear model (dashed line) that approximates the model well in the vicinity of X, but not necessarily globally.

在 github 页面上的各个链接中有更详细的信息。

treeinterpreter:http://blog.datadive.net/interpreting-random-forests/ 上提供了有关其工作原理的说明(这是用于回归;可以找到一个非常相似的分类示例,可以找到 here)。

简而言之:假设我们有一个比较特征的节点 F到某个值并基于此拆分实例。假设到达该节点的所有实例中有 50% 属于类 C .假设我们有一个新实例,它最终被分配给该节点的左子节点,现在所有实例的 80% 属于类 C .然后,特征的贡献F因为这个决定被计算为 0.8 - 0.5 = 0.3 (如果沿叶路径有更多节点也使用特征 F,则加上附加条款。

比较:需要注意的重要一点是,Lime 是一种独立于模型的方法(并非特定于决策树/RFs),它基于局部线性逼近。另一方面,Treeinterpreter 具体以与决策树本身类似的方式运行,并真正查看算法在比较中实际使用了哪些特征。所以他们实际上在做完全不同的事情。 Lime 说“一个特征很重要,如果我们稍微调整一下它就会产生不同的预测”。 Treeinterpreter 说“如果将某个特征与我们的一个节点中的阈值进行比较,它就会很重要,这会导致我们进行拆分,从而彻底改变了我们的预测”。


信任哪一个?

这很难明确回答。它们可能都以自己的方式有用。直觉上,你可能第一眼就倾向于 treeinterpreter,因为它是专门为决策树创建的。但是,请考虑以下示例:

  • 根节点:50% 的实例为 0 类,50% 为 1 类。IF F <= 50 , 向左走,否则向右走。
  • Left Child:48% 的实例为 0 类,52% 为 1 类。此子树下的子树。
  • 右 child :99% 的实例为 0 类,1% 的实例为 1 类。此子树下的子树。

如果大多数实例向左移动,只有一些向右移动,则这种设置是可能的。现在假设我们有一个带有 F = 49 的实例被分配到左侧并最终分配到第 1 类。Treeinterpreter 不会关心 F真的很接近在根节点中等式的另一边结束,并且只分配了 0.48 - 0.50 = -0.02 的低贡献. Lime 会注意到 F 的变化只需一点点就会完全改变赔率。

哪一个是对的?这还不是很清楚。你可以说F非常重要,因为如果它只有一点点不同,预测就会不同(然后石灰获胜)。你也可以争辩说F对我们的最终预测没有贡献,因为在检查了它的值(value)之后我们几乎没有接近一个决定,并且之后仍然需要研究许多其他特征。然后 treeinterpreter 获胜。

为了在此处获得更好的想法,实际绘制学习到的决策树本身也可能会有所帮助。然后,您可以手动遵循它的决策路径,并决定您认为哪些功能很重要和/或看看您是否可以理解为什么 Lime 和 treeinterpreter 会说他们所说的。

关于python - Lime vs TreeInterpreter 用于解释决策树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48909418/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com