XGBoost - n_estimators = 1 等于单树分类器?

XGBoost - n_estimators = 1 equal to single-tree classifier?

我有一些大量使用 XGBoost 而不是 scikit-learn 的训练管道,只是因为 XGBoost 干净地处理空值的方式。

但是,我的任务是向非技术人员介绍机器学习,我认为采用单树分类器的想法并讨论 XGBoost 一般 采用该数据结构 "puts it on steroids." 具体来说,我想绘制这个单树分类器以显示切割点。

指定 n_estimators=1 大致 等同于使用 scikit 的 DecisionTreeClassifier 吗?

import subprocess

import numpy as np
from xgboost import XGBClassifier, plot_tree

from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn import metrics

import matplotlib.pyplot as plt

RANDOM_STATE = 100
params = {
    'max_depth': 5,
    'min_samples_leaf': 5,
    'random_state': RANDOM_STATE
}

X, y = make_classification(
    n_samples=1000000,
    n_features=5,
    random_state=RANDOM_STATE
)

Xtrain, Xtest, ytrain, ytest = train_test_split(X, y, random_state=RANDOM_STATE)

# __init__(self, max_depth=3, learning_rate=0.1,
# n_estimators=100, silent=True,
# objective='binary:logistic', booster='gbtree',
# n_jobs=1, nthread=None, gamma=0,
# min_child_weight=1, max_delta_step=0,
# subsample=1, colsample_bytree=1, colsample_bylevel=1,
# reg_alpha=0, reg_lambda=1, scale_pos_weight=1,
# base_score=0.5, random_state=0, seed=None, missing=None, **kwargs)
xgb_model = XGBClassifier(
    n_estimators=1,
    max_depth=3,
    min_samples_leaf=5,
    random_state=RANDOM_STATE
)

# __init__(self, criterion='gini',
# splitter='best', max_depth=None,
# min_samples_split=2, min_samples_leaf=1,
# min_weight_fraction_leaf=0.0, max_features=None,
# random_state=None, max_leaf_nodes=None,
# min_impurity_decrease=0.0, min_impurity_split=None,
# class_weight=None, presort=False)
sk_model = DecisionTreeClassifier(
    max_depth=3,
    min_samples_leaf=5,
    random_state=RANDOM_STATE
)

xgb_model.fit(Xtrain, ytrain)
xgb_pred = xgb_model.predict(Xtest)

sk_model.fit(Xtrain, ytrain)
sk_pred = sk_model.predict(Xtest)

print(metrics.classification_report(ytest, xgb_pred))
print(metrics.classification_report(ytest, sk_pred))

plot_tree(xgb_model, rankdir='LR'); plt.show()

export_graphviz(sk_model, 'sk_model.dot'); subprocess.call('dot -Tpng sk_model.dot -o sk_model.png'.split())

一些性能指标(我知道,我没有完全校准分类器)...

>>> print(metrics.classification_report(ytest, xgb_pred))
              precision    recall  f1-score   support

           0       0.86      0.82      0.84    125036
           1       0.83      0.87      0.85    124964

   micro avg       0.85      0.85      0.85    250000
   macro avg       0.85      0.85      0.85    250000
weighted avg       0.85      0.85      0.85    250000

>>> print(metrics.classification_report(ytest, sk_pred))
              precision    recall  f1-score   support

           0       0.86      0.82      0.84    125036
           1       0.83      0.87      0.85    124964

   micro avg       0.85      0.85      0.85    250000
   macro avg       0.85      0.85      0.85    250000
weighted avg       0.85      0.85      0.85    250000

还有一些图片:

因此,除非进行任何调查 mistakes/overgeneralizations,否则 XGBClassifier(并且我假设是回归量)与一个估计器 似乎与 scikit- 相同使用相同的共享参数学习 DecisionTreeClassifier

如果你输入 n_estimators=1 这将是决策树的工作原理。有几种分割节点的方法(比如 gini-indexentropy),我不确定 scikit-learn 使用哪一个,哪个一个xgboost使用,但没关系。

您想展示构建决策树的核心特征和深层思想。我推荐 Patrick Winston 教授的 following Lecture。我自己用它来向我的同事演示决策树是如何工作的,而且效果很好。

然后,您可以将 Boosting 的想法添加到组合中。 Patrick 也讲授了它 in here.

设置 XGBoost n_estimators=1 使算法生成单树(基本上没有提升发生),这类似于 sklearn - DecisionTreeClassifier 的单树算法。

但是,两者可以调整的超参数和树生成过程是不同的。虽然 sklearn DecisionTreeClassifier 允许您调整比 xgboost 更多的超参数,但 xgboost 在超参数调整后会产生更好的准确性。 xgboost 生成的单个树优于 sklearn DecisionTreeClassifier 生成的单个树。

xgboost 的另一个优点是它可以自行处理缺失值。在 DecisionTreeClassifier 中,我们必须显式定义一个函数来处理可能产生不同结果的缺失值。

所以,在 sklearn DecisionTreeClassifier 上选择 n_estimators=1 的 xgboost!