GradientBoostingClassifier的apply函数混淆了

Lin*_* Ma 7 python scikit-learn

有关应用功能,请参阅此处

我的混淆更多来自这个示例,我在下面的代码片段中添加了一些打印输出更多的调试信息,

grd = GradientBoostingClassifier(n_estimators=n_estimator)
grd_enc = OneHotEncoder()
grd_lm = LogisticRegression()
grd.fit(X_train, y_train)
test_var = grd.apply(X_train)[:, :, 0]
print "test_var.shape", test_var.shape
print "test_var", test_var
grd_enc.fit(grd.apply(X_train)[:, :, 0])
grd_lm.fit(grd_enc.transform(grd.apply(X_train_lr)[:, :, 0]), y_train_lr)
Run Code Online (Sandbox Code Playgroud)

输出是像下面,和困惑是什么样的数字6.,3.并且10.是什么意思?以及它们与最终分类结果的关系如何?

test_var.shape (20000, 10)
test_var [[  6.   6.   6. ...,  10.  10.  10.]
 [ 10.  10.  10. ...,   3.   3.   3.]
 [  6.   6.   6. ...,  11.  10.  10.]
 ..., 
 [  6.   6.   6. ...,  10.  10.  10.]
 [  6.   6.   6. ...,  11.  10.  10.]
 [  6.   6.   6. ...,  11.  10.  10.]]
Run Code Online (Sandbox Code Playgroud)

Dav*_*ale 4

要了解梯度提升,您首先需要了解各个树。我将展示一个小例子。

设置如下:在 Iris 数据集上训练的小型 GB 模型来预测花朵是否属于第 2 类。

# import the most common dataset
from sklearn.datasets import load_iris
from sklearn.ensemble import GradientBoostingClassifier
X, y = load_iris(return_X_y=True)
# there are 150 observations and 4 features
print(X.shape) # (150, 4)
# let's build a small model = 5 trees with depth no more than 2
model = GradientBoostingClassifier(n_estimators=5, max_depth=2, learning_rate=1.0)
model.fit(X, y==2) # predict 2nd class vs rest, for simplicity
# we can access individual trees
trees = model.estimators_.ravel()
print(len(trees)) # 5
# there are 150 observations, each is encoded by 5 trees, each tree has 1 output
applied = model.apply(X) 
print(applied.shape) # (150, 5, 1)
print(applied[0].T) # [[2. 2. 2. 5. 2.]] - a single row of the apply() result
print(X[0]) # [5.1 3.5 1.4 0.2] - the pbservation corresponding to that row
print(trees[0].apply(X[[0]])) # [2] - 2 is the result of application the 0'th tree to the sample
print(trees[3].apply(X[[0]])) # [5] - 5 is the result of application the 3'th tree to the sample
Run Code Online (Sandbox Code Playgroud)

[2. 2. 2. 5. 2.]您可以看到生成的序列中的每个数字model.apply()对应于一棵树的输出。但这些数字意味着什么?

我们可以通过目视检查轻松分析决策树。这是一个绘制一个函数

# a function to draw a tree. You need pydotplus and graphviz installed 
# sudo apt-get install graphviz
# pip install pydotplus

from sklearn.externals.six import StringIO  
from IPython.display import Image  
from sklearn.tree import export_graphviz
import pydotplus
def plot_tree(clf):
    dot_data = StringIO()
    export_graphviz(clf, out_file=dot_data, node_ids=True,
                    filled=True, rounded=True, 
                    special_characters=True)
    graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
    return Image(graph.create_png())

# now we can plot the first tree
plot_tree(trees[0])
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

您可以看到每个节点都有一个编号(从 0 到 6)。如果我们将单个示例推入这棵树中,它将首先转到节点#1(因为该特征x3具有值0.2 < 1.75),然后到达节点#2(因为该特征x2具有值1.4 < 4.95)。

以同样的方式,我们可以分析产生输出的树 3 5

plot_tree(trees[3])
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这里我们的观察首先到节点#4,然后到节点#5,因为x1=3.5>2.25x2=1.4<4.85。因此,它最终得到数字 5。

就这么简单!产生的每个数字apply()都是样本最终所在的相应树的节点的序号。

这些数字与最终分类结果的关系是通过value相应树中叶子的关系来实现的。在二元分类的情况下,value所有叶子只是相加,如果是正数,则“正”类获胜,否则“负”类获胜。在多类分类的情况下,每个类的值相加,总值最大的类获胜。

在我们的例子中,第一棵树(及其节点#2)给出值-1.454,​​其他树也给出一些值,它们的总和是-4.84。它是负数,因此我们的例子不属于第 2 类。

values = [trees[i].tree_.value[int(leaf)][0,0] for i, leaf in enumerate(applied[0].ravel())]
print(values) # [-1.454, -1.05, -0.74, -1.016, -0.58] - the values of nodes [2,2,2,5,2] in the corresponding trees
print(sum(values)) # -4.84 - sum of these values is negative -> this is not class 2
Run Code Online (Sandbox Code Playgroud)