GBDT梯度提升树算法及官方案例

GBDT梯度提升树算法及官方案例
GBDT分类实战总结
loss: 选择损失函数,默认值为ls(least squres)
learning_rate: 学习率,模型是0.1
n_estimators: 弱学习器的数目,默认值100
max_depth: 每一个学习器的最大深度,限制回归树的节点数目,默认为3
min_samples_split: 可以划分为内部节点的最小样本数,默认为2
min_samples_leaf: 叶节点所需的最小样本数,默认为1

梯度提升树是一种决策树的集成算法。它通过反复迭代训练决策树来最小化损失函数。决策树类似,梯度提升树具有可处理类别特征、易扩展到多分类问题、不需特征缩放等性质。Spark.ml通过使用现有decision tree工具来实现。

梯度提升树依次迭代训练一系列的决策树。在一次迭代中,算法使用现有的集成来对每个训练实例的类别进行预测,然后将预测结果与真实的标签值进行比较。通过重新标记,来赋予预测结果不好的实例更高的权重。所以,在下次迭代中,决策树会对先前的错误进行修正。

对实例标签进行重新标记的机制由损失函数来指定。每次迭代过程中,梯度迭代树在训练数据上进一步减少损失函数的值。spark.ml为分类问题提供一种损失函数(Log Loss),为回归问题提供两种损失函数(平方误差与绝对误差)。

Spark.ml支持二分类以及回归的随机森林算法,适用于连续特征以及类别特征。不支持多分类问题。
在这里插入图片描述

import numpy as np
from sklearn.datasets import load_boston
from sklearn.utils import shuffle
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt

boston=load_boston()
data,target=shuffle(boston.data,boston.target,random_state=13)
data=data.astype(np.float32)
offset=int(data.shape[0]*0.9)
X_train,y_train=data[:offset],target[:offset]
X_test,y_test=data[offset:],target[offset:]

print('训练回归模型**********************************************************')
#随便指定参数长度,也不用在传参的时候去特意定义一个数组传参
params = {'n_estimators': 500, 'max_depth': 4, 'min_samples_split': 2,
          'learning_rate': 0.01, 'loss': 'ls'}
gb=GradientBoostingRegressor(**params)
gb.fit(X_train,y_train)
# ean_squared_error:
# 均方误差是指参数估计值与参数真值之差平方的期望值;
# MSE可以评价数据的变化程度,MSE的值越小,说明预测模型描述实验数据具有更好的精确度。
mse=mean_squared_error(y_test,gb.predict(X_test))
print("MSE:%.4f"%mse)


print('比较测试误差**********************************************************')
test_score=np.zeros((params['n_estimators'],),dtype=np.float64)
# staged_predict(X):返回每个基分类器的预测数据集X的结果。
# print(gb.staged_predict(X_test))
for i,y_pred in enumerate(gb.staged_predict(X_test)):
    test_score[i]=gb.loss_(y_test,y_pred)
plt.figure(figsize=(12,6))
plt.subplot(1,2,1)
plt.title("Deviance")
plt.plot(np.arange(params['n_estimators'])+1,gb.train_score_,'b-',label='train_deviance_score')
plt.plot(np.arange(params['n_estimators'])+1,test_score,'r-',label='test_deviance_score')
plt.legend(loc='upper right')
plt.xlabel("Boosting Iterations")
plt.ylabel('Deviance')


print('画出属性重要性*********************************************************')
feature_importance=gb.feature_importances_
# print(feature_importance)
feature_importance=100.0*(feature_importance/max(feature_importance))
# print(feature_importance)
#升序排序
sorted_idx=np.argsort(feature_importance)
pos=np.arange(sorted_idx.shape[0])+0.5
plt.subplot(1,2,2)
plt.barh(pos,feature_importance[sorted_idx],align="center")
plt.title('Variable Importance')
plt.xlabel('Relative Importance')
plt.yticks(pos, boston.feature_names[sorted_idx])
plt.show()
训练回归模型**********************************************************
MSE:6.6274
比较测试误差**********************************************************
画出属性重要性*********************************************************

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值