GBDT 算法【python,机器学习,算法】

GBDT 即 Gradient Boosting Decision Tree 梯度提升树, 是一种迭代的决策树算法,又叫 MART(Multiple Additive Regression Tree),

它通过构造一组弱的学习器(树),然后把多棵决策树的结果累加起来作为最终的预测输出。该算法将决策树与集成思想进行了有效的结合。具体实现步骤如下:

  1. 初始化基分类器。
  2. 以当前学习器的预测值为准,计算未正确预测的样本(即残差)。
  3. 使用残差构建下一棵决策树(主要思想:试图纠正前一个模型的错误,使其不断提升预测正确率)。
  4. 重复 2-3 步骤,直到满足终止条件为止(误差很小或者达到一定的迭代次数),结束迭代。
  5. 将迭代中的每个分类器产生的预测值相加,得到最终的预测结果。

下面是一个简单的示例,使用梯度提升算法和决策树分类器对手写数字数据进行对比分析:

python 复制代码
# 导入sklearn内置数据集
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits

# 导入手写数字数据
digits = load_digits()

plt.figure(1, figsize=(3.5, 3.5), facecolor='white')
for i in range(10):
	for j in range(10):
		ax = plt.subplot(10, 10, 10 * i + j + 1)
		# 设置子图的位置
		ax.set_xticks([])
		# 隐藏横坐标
		# 隐藏纵坐标
		ax.set_yticks([])
		plt.imshow(digits.images[9 * i + j], cmap=plt.cm.gray_r,
		           interpolation="nearest")
plt.show()

# 导入sklearn中的模型验证类
from sklearn.model_selection import train_test_split

# 使用train test_split函数自动分割训练数据集和测试数据集
x_train, x_test, y_train, y_test = train_test_split(digits.data, digits.target,
                                                    test_size=0.3)
# 导入sklearn模块中的决策树分类器类
from sklearn.tree import DecisionTreeClassifier

# 定义一个决策树分类器对象
dtc = DecisionTreeClassifier()
dtc.fit(x_train, y_train)
# 导入sklearn模块中的梯度提升分类器类
from sklearn.ensemble import GradientBoostingClassifier

# 定义一个梯度提升决策树分类器对象
gbc = GradientBoostingClassifier(n_estimators=30, learning_rate=0.8)
gbc.fit(x_train, y_train)
print("单棵决策树在训练集上的性能:%.3f" % dtc.score(x_train, y_train))
print("单棵决策树在测试集上的性能:%.3f" % dtc.score(x_test, y_test))
print("GBDT(T-30)在训练集上的性能:%.3f" % gbc.score(x_train, y_train))
print("GBDT(T-30)在测试集上的性能:%.3f" % gbc.score(x_test, y_test))
# 观察弱分类器数量对分类准确度的影响
# 弱分类器的最大值
T_max = 39
gbc_train_scores = []
gbc_test_scores = []
for i in range(1, T_max + 1):
	gbc = GradientBoostingClassifier(n_estimators=i, learning_rate=0.1)
	gbc.fit(x_train, y_train)
	gbc_train_scores.append(gbc.score(x_train, y_train))
	gbc_test_scores.append(gbc.score(x_test, y_test))

# 绘制测试结果
import matplotlib.pyplot as plt

# 解决图形中的中文显示乱码
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.matplotlib.rcParams['axes.unicode_minus'] = False
plt.figure()
# 解决图形中的坐标轴负号显示问题
plt.plot(range(1, T_max + 1), gbc_train_scores, color='r', label='训练集')
plt.plot(range(1, T_max + 1), gbc_test_scores, color='g', label='测试集')
plt.title("基学习器数量对GBDT性能的影响")
plt.xlabel("基分类器数量")
plt.ylabel("准确率")
plt.xlim(1, T_max)
plt.legend()
plt.show()

上面的代码演示了基学习器的数量对 GBDT 性能的影响。主要步骤如下:

  1. 导入训练数据。
  2. 将数据切分为两个集合:训练集和测试集。
  3. 使用不同数量的学期器对数据集进行拟合训练和预测。
  4. 绘制基学习器数量对 GBDT 性能的影响图像。

你可以根据实际需要对代码中的数据进行调整以适应不同的测试需要。

你可以根据实际需要对代码中的数据进行调整以适应不同的测试需要。

相关推荐
BirdenT22 分钟前
20260519紫题训练
c++·算法
Li emily5 小时前
解决了加密货币api多币种订阅时的数据乱序问题
人工智能·python·api·fastapi
csdn_aspnet5 小时前
C语言 Lomuto分区算法(Lomuto Partition Algorithm)
c语言·开发语言·算法
2301_781571426 小时前
Golang格式化输出占位符都有什么_Golang fmt占位符教程【通俗】
jvm·数据库·python
asdzx676 小时前
使用 Python 为 PDF 添加页码 (详细教程)
python·pdf·页码
谙弆悕博士6 小时前
【附C源码】从零实现C语言堆数据结构:原理、实现与应用
c语言·数据结构·算法··数据结构与算法
AI技术控6 小时前
《Transformers are Inherently Succinct》论文解读:从“能表达什么”到“多紧凑地表达”
人工智能·python·深度学习·机器学习·自然语言处理
orion-orion7 小时前
学习理论:在线弃权学习
机器学习·统计学习·学习理论
金融大 k8 小时前
Python 全球指数监控面板:TickDB + REST + WebSocket 完整方案
python·websocket
啊哈哈121388 小时前
系统设计复盘:为什么 Agent 的 ReAct 循环必须内嵌确定性保护层——以 FitMind 健康助手的路由与步骤控制为例
人工智能·python·react