【Python机器学习】决策树集成——梯度提升回归树

理论知识:

梯度提升回归树通过合并多个决策树来构建一个更为强大的模型。虽然名字里有"回归",但这个模型既能用于回归,也能用于分类。与随机森林方法不同,梯度提升采用连续的方式构造树,每棵树都试图纠正前一棵树的错误。默认情况下,梯度提升回归树中没有随机化,而是用到了强预剪枝。梯度提升树通常使用深度很小(1-5之间),这样的模型占用内存小,预测速度也更快。

梯度提升背后的主要思想是合并许多简单的模型(弱学习器),比如深度较小的树。每棵树只能对部分数据做出比较好的预测,因此添加的树越来越多,可以不断迭代来提高性能。

梯度提升树通常对参数设置非常敏感,但如果参数设置正确的话,模型精度会更高。

除了预剪枝和集成树的数量外,梯度提升的另一个重要参数是learning_rate(学习率),用于控制每棵树纠正前一棵树的错误的强度。较高的学习率意味着每棵树都可以做出较强的修正,这样的模型更为复杂。通过增大n_estimators来向集成中添加更多树,也可以增加模型的复杂度,因为模型有更多机会来纠正训练集上的错误。

默认参数上:树的数量为100、最大深度为3,学习率为0.1

示例:

以乳腺癌数据集为例,用分类模型:

python 复制代码
from sklearn.ensemble import RandomForestClassifier,GradientBoostingClassifier
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer
import numpy as np

plt.rcParams['font.sans-serif'] = ['SimHei']
cancer=load_breast_cancer()
X_train,X_test,y_train,y_test=train_test_split(cancer.data,cancer.target,random_state=0)
gbrt=GradientBoostingClassifier(random_state=0)
gbrt.fit(X_train,y_train)
print('训练集精度:{:.3f}'.format(gbrt.score(X_train,y_train)))
print('测试集精度:{:.3f}'.format(gbrt.score(X_test,y_test)))

由于训练集精度达到100%,所以很可能存在过拟合,为了降低过拟合,可以限制最大深度来加强预剪枝,也可以降低学习率:

python 复制代码
gbrt_md1=GradientBoostingClassifier(random_state=0,max_depth=1)
gbrt_md1.fit(X_train,y_train)
print('max_depth=1训练集精度:{:.3f}'.format(gbrt_md1.score(X_train,y_train)))
print('max_depth=1测试集精度:{:.3f}'.format(gbrt_md1.score(X_test,y_test)))

gbrt_lr001=GradientBoostingClassifier(random_state=0,learning_rate=0.01)
gbrt_lr001.fit(X_train,y_train)
print('learning_rate=0.01训练集精度:{:.3f}'.format(gbrt_lr001.score(X_train,y_train)))
print('learning_rate=0.01测试集精度:{:.3f}'.format(gbrt_lr001.score(X_test,y_test)))

可以看到,两种方法都降低了训练集精度,而减小树的最大深度显著提升了模型性能。

特征重要性可视化:

python 复制代码
import mglearn.plots
from sklearn.ensemble import RandomForestClassifier,GradientBoostingClassifier
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer
import numpy as np

def plot_importances(model):
    n_feature=cancer.data.shape[1]
    plt.barh(range(n_feature),model.feature_importances_,align='center')
    plt.yticks(np.arange(n_feature),cancer.feature_names)
    plt.xlabel('特征重要性')
    plt.ylabel('特征')

plt.rcParams['font.sans-serif'] = ['SimHei']
cancer=load_breast_cancer()
X_train,X_test,y_train,y_test=train_test_split(cancer.data,cancer.target,random_state=0)
gbrt=GradientBoostingClassifier(random_state=0)
gbrt.fit(X_train,y_train)

plot_importances(gbrt)
plt.show()

可以看到,梯度提升树的特征重要性与随机森林有些类似,但梯度提升树完全忽略了某些特征。

常用的方法是先尝试随机森林,因为它的鲁棒性很好,如果随机森林的效果好但预测时间太长,或者学习模型精度在小数点后两位的提高也很重要,那么切换成梯度提升树通常比较有用。

优缺点:

梯度提升树是监督学习中最强大也最常用的模型之一,它的主要缺点是需要仔细调参,而且训练时间会比较长;优点是不需要对数据进行缩放就可以表现的很好,而且也适用于二元特征和连续特征同时存在的数据集。与其他基于树的模型相同,梯度提升树通常也不适用于高纬稀疏数据。

梯度提升树的主要参数是树的数量n_estimators和学习率learning_rate。这两个参数高度相关,因为learning_rate越低,就需要更多树来构建具有相似复杂度的模型,随机森林的n_estimators值总是越大越好,但梯度提升树不同,增大n_estimators会导致模型更加复杂,进而可能导致过拟合,通常的做法是根据时间和内存的预算选择合适的n_estimators,然后会不同的learning_rate进行遍历。

另一个重要参数是max_depth,用来降低每棵树的复杂度,一般不超过5。

相关推荐
陈广亮11 分钟前
构建具有长期记忆的 AI Agent:从设计模式到生产实践
人工智能
会写代码的柯基犬20 分钟前
DeepSeek vs Kimi vs Qwen —— AI 生成俄罗斯方块代码效果横评
人工智能·llm
Mintopia1 小时前
OpenClaw 是什么?为什么节后热度如此之高?
人工智能
爱可生开源社区1 小时前
DBA 的未来?八位行业先锋的年度圆桌讨论
人工智能·dba
叁两4 小时前
用opencode打造全自动公众号写作流水线,AI 代笔太香了!
前端·人工智能·agent
敏编程4 小时前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪4 小时前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
strayCat232554 小时前
Clawdbot 源码解读 7: 扩展机制
人工智能·开源
王鑫星4 小时前
SWE-bench 首次突破 80%:Claude Opus 4.5 发布,Anthropic 的野心不止于写代码
人工智能