Skip to main content
 首页 » 编程设计

python之我可以使用 XGBoost 来提升其他模型(例如朴素贝叶斯、随机森林)吗

2025年05月04日22TianFang

我正在从事一个欺诈分析项目,我需要一些提升方面的帮助。之前,我使用 SAS Enterprise Miner 来了解更多关于增强/集成技术的知识,我了解到增强可以帮助提高模型的性能。

目前,我的团队已经在 Python 上完成了以下模型:朴素贝叶斯、随机森林和神经网络。我们想使用 XGBoost 来提高 F1 分数。我不确定这是否可行,因为我只看过有关如何单独执行 XGBoost 或朴素贝叶斯的教程。

我正在寻找教程,他们将在其中向您展示如何创建朴素贝叶斯模型,然后使用提升。之后,我们可以比较有和没有提升的指标,看看它是否有所改善。我对机器学习还比较陌生,所以我对这个概念的理解可能是错误的。

我想替换 XGBoost 中的值,但不确定要更改哪个值,或者它是否可以这样工作。

朴素贝叶斯

from sklearn.model_selection import train_test_split 
X_train, X_test, y_train, y_test = train_test_split(X_sm,y_sm, test_size = 0.2, random_state=0) 
 
from sklearn.naive_bayes import GaussianNB 
from sklearn.metrics import confusion_matrix, confusion_matrix, accuracy_score, f1_score, precision_score, recall_score 
 
nb = GaussianNB() 
nb.fit(X_train, y_train) 
nb_pred = nb.predict(X_test) 

XGBoost

from sklearn.model_selection import train_test_split 
import xgboost as xgb 
from xgboost import XGBClassifier 
from sklearn.model_selection import train_test_split 
 
X_train, X_test, y_train, y_test = train_test_split(X_sm,y_sm, test_size = 0.2, random_state=0) 
model = XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1, 
colsample_bynode=1, colsample_bytree=0.9, gamma=0, 
learning_rate=0.1, max_delta_step=0, max_depth=10, 
min_child_weight=1, missing=None, n_estimators=500, n_jobs=-1, 
nthread=None, objective='binary:logistic', random_state=0, 
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None, 
silent=None, subsample=0.9, verbosity=0) 
 
model.fit(X_train, y_train) 
y_pred = model.predict(X_test) 
predictions = [round(value) for value in y_pred] 

请您参考如下方法:

在理论中,使用 scikit-learn 的 AdaBoostClassifier 可以简单直接地提升任何(基础)分类器.例如。对于朴素贝叶斯分类器,它应该是:

from sklearn.ensemble import AdaBoostClassifier 
from sklearn.naive_bayes import GaussianNB 
 
nb = GaussianNB() 
model = AdaBoostClassifier(base_estimator=nb, n_estimators=10) 
model.fit(X_train, y_train) 

等等。

在实践中,我们从不使用朴素贝叶斯或神经网络作为增强的基础分类器(更不用说随机森林了,它们本身就是一种集成方法)。

Adaboost(以及后来派生的类似提升方法,如 GBM 和 XGBoost)是使用决策树 (DT) 作为基础分类器(更具体地说,决策树桩,即具有深度的 DT)只有 1 个);有充分的理由说明为什么直到今天,如果您没有在上面的 scikit-learn 的 AdaBoostClassifier 中明确指定 base_classifier 参数,它会假定值为 DecisionTreeClassifier(max_depth =1),即决策树桩。

DT 适合此类集成,因为它们本质上是不稳定 分类器,而提到的其他算法并非如此,因此后者在用作增强的基础分类器时不会提供任何东西算法。