【sklearn】Toy datasets上的分类/回归问题 (XGBoost实践)

分类问题

1. 手写数字识别问题
from sklearn.datasets import load_digits

digits = load_digits()  # 加载手写字符识别数据集
X = digits.data  # 特征值
y = digits.target  # 目标值

X.shape, y.shape

((1797, 64), (1797,))

划分70%训练集,30%测试集,

from sklearn.model_selection import train_test_split

# 划分数据集,70% 训练数据和 30% 测试数据
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

X_train.shape, X_test.shape, y_train.shape, y_test.shape

((1257, 64), (540, 64), (1257,), (540,))

使用默认参数,

import xgboost as xgb

model_c = xgb.XGBClassifier()
model_c

XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0, learning_rate=0.1,
max_delta_step=0, max_depth=3, min_child_weight=1, missing=None,
n_estimators=100, 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=1, verbosity=1)

参数解释:
max_depth – 基学习器的最大树深度。
learning_rate – Boosting 学习率。
n_estimators – 决策树的数量。
gamma – 惩罚项系数,指定节点分裂所需的最小损失函数下降值。
booster – 指定提升算法:gbtree, gblinear or dart。
n_jobs – 指定多线程数量。
reg_alpha – L1 正则权重。
reg_lambda – L2 正则权重。
scale_pos_weight – 正负权重平衡。
random_state – 随机数种子。

model_c.fit(X_train, y_train)  # 使用训练数据训练
model_c.score(X_test, y_test)  # 使用测试数据计算准确度

0.9592592592592593

简单调参:

max_depthresult
10.912962962962963
20.9666666666666667
30.9592592592592593
40.9629629629629629
50.9537037037037037
60.9537037037037037

max_depth=2时效果最好。

learning_rateresult
0.050.9148148148148149
0.10.9666666666666667
0.150.9777777777777777
0.20.9814814814814815
0.250.9851851851851852
0.30.9796296296296296
0.350.9851851851851852
0.40.9851851851851852

learning_rate设置为0.25比较好。

n_estimatorsresult
500.9722222222222222
750.9777777777777777
1000.9851851851851852
1250.987037037037037
1500.987037037037037
2000.987037037037037

n_estimators设置为125比较好。

经过调参,准确率从95.9%提高到了98.7%。

from matplotlib import pyplot as plt
from matplotlib.pylab import rcParams
%matplotlib inline

# 设置图像大小
rcParams['figure.figsize'] = [50, 10]

xgb.plot_tree(model_c, num_trees=1)

回归问题

1. 波士顿房价预测问题
from sklearn.datasets import load_boston

boston = load_boston()
X = boston.data  # 特征值
y = boston.target  # 目标值

# 划分数据集,80% 训练数据和 20% 测试数据
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

X_train.shape, X_test.shape, y_train.shape, y_test.shape

((404, 13), (102, 13), (404,), (102,))

默认参数:

model_r = xgb.XGBRegressor()
model_r

XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0,
importance_type='gain', learning_rate=0.1, max_delta_step=0,
max_depth=3, min_child_weight=1, missing=None, n_estimators=100,
n_jobs=1, nthread=None, objective='reg:linear', random_state=0,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,
silent=None, subsample=1, verbosity=1)

model_r.fit(X_train, y_train)  # 使用训练数据训练
model_r.score(X_test, y_test)  # 使用测试数据计算 R^2 评估指标

0.811524182952107

参考资料

scikit-learn toy datasets doc.

实验楼-XGBoost 梯度提升基础课程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值