Python机器学习

AdaBoost

AdaBoost详细操作教程
它是最成功的增强合奏算法之一。该算法的主要关键在于它们对数据集中的实例赋予权重的方式。因此,在构造后续模型时,该算法无需过多关注实例。
在以下Python食谱中,我们将通过使用Pima Indians糖尿病数据集上的 sklearn AdaBoostClassifier 类来构建Ada Boost集成模型进行分类。
首先,按如下所示导入所需的程序包-
# 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 AdaBoostClassifier
现在,我们需要像之前的示例一样加载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 = AdaBoostClassifier(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.7539473684210527
上面的输出表明,我们的AdaBoost分类器集成模型的准确度约为75%。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4