上海seo网站优化公司,内蒙做网站,烟台网站建设设计开发,广州网站(建设信科网络)GBDT算法详解
梯度提升决策树#xff08;Gradient Boosting Decision Trees#xff0c;GBDT#xff09;是机器学习中一种强大的集成算法。它通过构建一系列的决策树#xff0c;并逐步优化模型的预测能力#xff0c;在各种回归和分类任务中取得了显著的效果。本文将详细介…GBDT算法详解
梯度提升决策树Gradient Boosting Decision TreesGBDT是机器学习中一种强大的集成算法。它通过构建一系列的决策树并逐步优化模型的预测能力在各种回归和分类任务中取得了显著的效果。本文将详细介绍GBDT算法的原理并展示其在实际数据集上的应用。
GBDT算法原理
GBDT是一种集成学习方法通过逐步建立多个决策树每棵树都在前一棵树的基础上进行改进。GBDT的基本思想是逐步减少残差即预测误差使模型的预测能力不断提高。
算法步骤
初始化模型使用常数模型初始化比如回归问题中可以用目标值的均值初始化模型。计算残差计算当前模型的残差即预测值与真实值之间的差异。拟合残差用新的决策树拟合残差并更新模型。更新模型将新决策树的预测结果加到模型中以减少残差。重复步骤2-4直到达到预设的迭代次数或残差足够小。
公式表示
初始化模型 F 0 ( x ) arg min γ ∑ i 1 n L ( y i , γ ) F_0(x) \arg\min_{\gamma} \sum_{i1}^{n} L(y_i, \gamma) F0(x)argγmini1∑nL(yi,γ) 对于每一次迭代 (m 1, 2, \ldots, M) 计算负梯度残差 r i m − [ ∂ L ( y i , F ( x i ) ) ∂ F ( x i ) ] F ( x ) F m − 1 ( x ) r_{im} -\left[ \frac{\partial L(y_i, F(x_i))}{\partial F(x_i)} \right]_{F(x) F_{m-1}(x)} rim−[∂F(xi)∂L(yi,F(xi))]F(x)Fm−1(x) 拟合一个新的决策树来预测残差 h m ( x ) arg min h ∑ i 1 n ( r i m − h ( x i ) ) 2 h_m(x) \arg\min_{h} \sum_{i1}^{n} (r_{im} - h(x_i))^2 hm(x)arghmini1∑n(rim−h(xi))2 更新模型 F m ( x ) F m − 1 ( x ) ν h m ( x ) F_m(x) F_{m-1}(x) \nu h_m(x) Fm(x)Fm−1(x)νhm(x) 其中 ν \nu ν是学习率控制每棵树对最终模型的贡献。
GBDT算法的特点
高准确性GBDT通过逐步减少残差不断优化模型使其在很多任务中具有很高的准确性。灵活性GBDT可以处理回归和分类任务并且可以使用各种损失函数。鲁棒性GBDT对数据的噪声和异常值有一定的鲁棒性。可解释性决策树本身具有一定的可解释性通过特征重要性等方法可以解释GBDT模型。
GBDT参数说明
以下是GBDTGradient Boosting Decision Trees梯度提升决策树常用参数及其详细说明
参数名称描述默认值示例n_estimators树的棵数提升迭代的次数100n_estimators200learning_rate学习率控制每棵树对最终模型的贡献0.1learning_rate0.05max_depth树的最大深度控制每棵树的复杂度3max_depth4min_samples_split分裂一个内部节点需要的最少样本数2min_samples_split5min_samples_leaf叶子节点需要的最少样本数1min_samples_leaf3subsample样本采样比例用于训练每棵树1.0subsample0.8max_features寻找最佳分割时考虑的最大特征数Nonemax_featuressqrtloss要优化的损失函数deviancelossexponentialcriterion分裂节点的标准friedman_msecriterionmaeinit初始估计器Noneinitsome_estimatorrandom_state随机数种子用于结果复现Nonerandom_state42verbose控制训练过程信息的输出频率0verbose1warm_start是否使用上次调用的解决方案来初始化训练Falsewarm_startTruepresort是否预排序数据以加快分裂查找deprecated-validation_fraction用于提前停止训练的验证集比例0.1validation_fraction0.2n_iter_no_change如果在若干次迭代内验证集上的损失没有改善则提前停止训练Nonen_iter_no_change10tol提前停止的阈值1e-4tol1e-3ccp_alpha最小成本复杂度修剪参数0.0ccp_alpha0.01
通过合理调整这些参数可以优化GBDT模型在特定任务和数据集上的性能。
GBDT算法在回归问题中的应用
在本节中我们将使用波士顿房价数据集来展示如何使用GBDT算法进行回归任务。
导入库
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.metrics import mean_squared_error, r2_score
加载和预处理数据
# 生成合成回归数据集
X, y make_regression(n_samples1000, n_features20, noise0.1, random_state42)# 数据集划分
X_train, X_test, y_train, y_test train_test_split(X, y, test_size0.2, random_state42)# 数据标准化
scaler StandardScaler()
X_train scaler.fit_transform(X_train)
X_test scaler.transform(X_test)训练GBDT模型
# 训练GBDT模型
gbdt GradientBoostingRegressor(n_estimators100, learning_rate0.1, max_depth3, random_state42)
gbdt.fit(X_train, y_train)预测与评估
# 预测
y_pred gbdt.predict(X_test)# 评估
mse mean_squared_error(y_test, y_pred)
r2 r2_score(y_test, y_pred)
print(fMean Squared Error: {mse:.2f})
print(fR^2 Score: {r2:.2f})特征重要性
# 特征重要性
# 特征重要性
feature_importances gbdt.feature_importances_
plt.barh(range(X.shape[1]), feature_importances, aligncenter)
plt.yticks(np.arange(X.shape[1]), [fFeature {i} for i in range(X.shape[1])])
plt.xlabel(Feature Importance)
plt.ylabel(Feature)
plt.title(Feature Importances in GBDT)
plt.show()GBDT算法在分类问题中的应用
在本节中我们将使用20类新闻组数据集来展示如何使用GBDT算法进行文本分类任务。
导入库
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
加载和预处理数据
# 生成分类数据集
X, y make_classification(n_samples1000, n_features20, n_informative15, n_redundant5, random_state42)# 数据集划分
X_train, X_test, y_train, y_test train_test_split(X, y, test_size0.2, random_state42)# 数据标准化
scaler StandardScaler()
X_train scaler.fit_transform(X_train)
X_test scaler.transform(X_test)
训练GBDT模型
# 训练GBDT模型
gbdt GradientBoostingClassifier(n_estimators100, learning_rate0.1, max_depth3, random_state42)
gbdt.fit(X_train, y_train)
预测与评估
# 预测
y_pred gbdt.predict(X_test)# 评估
accuracy accuracy_score(y_test, y_pred)
print(fAccuracy: {accuracy:.2f})# 混淆矩阵
conf_matrix confusion_matrix(y_test, y_pred)
print(Confusion Matrix:)
print(conf_matrix)# 分类报告
class_report classification_report(y_test, y_pred)
print(Classification Report:)
print(class_report)
结语
本文详细介绍了GBDT算法的原理和特点并展示了其在回归和分类任务中的应用。首先介绍了GBDT算法的基本思想和公式然后展示了如何在回归数据集使用GBDT进行回归任务以及如何在分类数据集上使用GBDT进行文本分类任务。
我的其他同系列博客
支持向量机(SVM算法详解) knn算法详解 GBDT算法详解 XGBOOST算法详解 CATBOOST算法详解 随机森林算法详解 lightGBM算法详解 对比分析GBDT、XGBoost、CatBoost和LightGBM 机器学习参数寻优方法、实例与分析