Python机器学习

随机梯度提升

随机梯度提升详细操作教程
它也称为梯度提升机。在下面的Python食谱中,我们将通过使用pima Indians糖尿病数据集上的 sklearn GradientBoostingClassifier 类来建立随机梯度Boostingensemble模型进行分类。
首先,如下所示导入所需的程序包-
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-27
from pandas import read_csv
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import GradientBoostingClassifier
现在,我们需要像之前的示例一样加载Pima糖尿病数据集-
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-27
path = r"C:\pima-indians-diabetes.csv"
headernames = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = read_csv(path, names = headernames)
array = data.values
X = array[:,0:8]
Y = array[:,8]
接下来,如下所示进行10倍交叉验证的输入-
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-27
seed = 5
kfold = KFold(n_splits = 10, random_state = seed)
我们需要提供要建造的树木数量。在这里,我们正在构建150棵树木,这些树木的分裂点可以从5个特征中选择-
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-27
num_trees = 50
接下来,在以下脚本的帮助下构建模型-
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-27
model = GradientBoostingClassifier(n_estimators = num_trees, random_state = seed)
计算并打印结果如下-
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-27
results = cross_val_score(model, X, Y, cv = kfold)
print(results.mean())
输出
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-27
0.7746582365003418
上面的输出显示,我们的Gradient Boosting分类器集成模型的准确性约为77.5%。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4