gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:28:04 26 4
gpt4 key购买 nike

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

但是,我的任务是向非技术人员介绍机器学习,并且认为最好采用单树分类器的想法并讨论 XGBoost 通常该数据结构并“为其注入(inject)类固醇”。具体来说,我想绘制这个单树分类器来显示切点。

指定 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

还有一些图片:

scikit xgboost

因此,除非有任何调查错误/过度概括,XGBClassifier (并且,我认为回归器)带有一个估计器似乎与 scikit-learn 相同 DecisionTreeClassifier具有相同的共享参数。

关于machine-learning - XGBoost - n_estimators = 1 等于单树分类器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53230138/

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