当前位置:网站首页>机器学习模型融合大法!
机器学习模型融合大法!
2022-04-22 14:34:00 【Datawhale】
集成学习基础
集成学习是指结合两个或多个模型的机器学习模型。集成学习是机器学习的分支,通常在追求更强预测的能力时使用。
集成学习经常被机器学习竞赛中的顶级和获胜参与者使用。现代机器学习库(scikit-learn、XGBoost)内部已经结合了常见的集成学习方法。
集成学习介绍
集成学习结合多个不同的模型,然后结合单个模型完成预测。通常情况下,集成学习能比单个模型找到更好的性能。
常见的集成学习技术有三类:
Bagging, 如. Bagged Decision Trees and Random Forest.
Boosting, 如. Adaboost and Gradient Boosting
Stacking, 如. Voting and using a meta-model.
使用集成学习可以减少预测结果的方差,同时也比单个模型更好的性能。
Bagging
Bagging通过采样训练数据集的样本,训练得到多样的模型,进而得到多样的预测结果。在结合模型的预测结果时,可以对单个模型预测结果进行投票或平均。

Bagging的关键是对数据集的采样方法。常见的方式可以从行(样本)维度进行采样,这里进行的是有放回采样。
Bagging可通过BaggingClassifier和BaggingRegressor使用,默认情况下它们使用决策树作为基本模型,可以通过n_estimators参数指定要创建的树的数量。
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.ensemble import BaggingClassifier
# 创建样例数据集
X, y = make_classification(random_state=1)
# 创建bagging模型
model = BaggingClassifier(n_estimators=50)
# 设置验证集数据划分方式
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# 验证模型精度
n_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
# 打印模型的精度
print('Mean Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))
Random Forest
随机森林是 Bagging与树模型的结合:
随机森林集成在训练数据集的不同引导样本上拟合决策树。
随机森林还将对每个数据集的特征(列)进行采样。
在构建每个决策树时,随机森林不是在选择分割点时考虑所有特征,而是将特征限制为特征的随机子集。
随机森林集成可通过RandomForestClassifier和RandomForestRegressor类在 scikit-learn 中获得。您可以通过n_estimators参数指定要创建的树的数量,并通过max_features参数指定要在每个分割点考虑的随机选择的特征的数量。
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.ensemble import RandomForestClassifier
# 创建样例数据集
X, y = make_classification(random_state=1)
# 创建随机森林模型
model = RandomForestClassifier(n_estimators=50)
# 设置验证集数据划分方式
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# 验证模型精度
n_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
# 打印模型的精度
print('Mean Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))
AdaBoost
Boosting在迭代过程中尝试纠先前模型所产生的错误,迭代次数越多集成产生的错误就越少,至少在数据支持的限制范围内并且在过度拟合训练数据集之前。

Boosting想法最初是作为一种理论思想发展起来的,AdaBoost算法是第一个成功实现基于Boosting的集成算法的方法。
AdaBoost在加权训练数据集的版本上拟合决策树,以便树更多地关注先前成员出错的示例。AdaBoost不是完整的决策树,而是使用非常简单的树,在做出预测之前对一个输入变量做出单一决策。这些短树被称为决策树桩。
AdaBoost可通过AdaBoostClassifier和AdaBoostRegressor使用,它们默认使用决策树(决策树桩)作为基本模型,可以通过n_estimators参数指定要创建的树的数量。
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.ensemble import AdaBoostClassifier
# 创建样例数据集
X, y = make_classification(random_state=1)
# 创建adaboost模型
model = AdaBoostClassifier(n_estimators=50)
# 设置验证集数据划分方式
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# 验证模型精度
n_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
# 打印模型的精度
print('Mean Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))
Gradient Boosting
Gradient Boosting是一个用于提升集成算法的框架,是对AdaBoosting的扩展。Gradient Boosting定义为统计框架下的加法模型,并允许使用任意损失函数以使其更加灵活,并允许使用损失惩罚(收缩)来减少过度拟合。
Gradient Boosting引入了Bagging的操作,例如训练数据集行和列的采样,称为随机梯度提升。
对于结构化或表格数据来说,Gradient Boosting一种非常成功的集成技术,尽管由于模型是按顺序添加的,因此拟合模型可能会很慢。已经开发了更有效的实现,如XGBoost、LightGBM。
Gradient Boosting在可以通过GradientBoostingClassifier和GradientBoostingRegressor使用,默认使用决策树作为基础模型。您可以通过n_estimators参数指定要创建的树的数量,通过learning_rate参数控制每棵树的贡献的学习率。
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.ensemble import GradientBoostingClassifier
# 创建样例数据集
X, y = make_classification(random_state=1)
# 创建GradientBoosting模型
model = GradientBoostingClassifier(n_estimators=50)
# 设置验证集数据划分方式
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# 验证模型精度
n_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
# 打印模型的精度
print('Mean Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))
Voting
Voting使用简单的统计数据来组合来自多个模型的预测。
硬投票:对预测类别进行投票;
软投票:对预测概率进行求均值;
Voting可通过VotingClassifier和VotingRegressor使用。可以将基本模型列表作为参数,列表中的每个模型都必须是具有名称和模型的元组,
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.ensemble import VotingClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression
# 创建数据集
X, y = make_classification(random_state=1)
# 模型列表
models = [('lr', LogisticRegression()), ('nb', GaussianNB())]
# 创建voting模型
model = VotingClassifier(models, voting='soft')
# 设置验证集数据划分方式
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# 验证模型精度
n_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
# 打印模型的精度
print('Mean Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))
Stacking
Stacking组合多种不同类型的基本模型的预测,和Voting类似。但Stacking可以根据验证集来调整每个模型的权重。

Stacking需要和交叉验证搭配使用,也可以通过StackingClassifier和StackingRegressor使用,可以将基本模型作为模型的参数提供。
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.ensemble import StackingClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
# 创建数据集
X, y = make_classification(random_state=1)
# 模型列表
models = [('knn', KNeighborsClassifier()), ('tree', DecisionTreeClassifier())]
# 设置验证集数据划分方式
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# 验证模型精度
n_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
# 打印模型的精度
print('Mean Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))

干货学习,点赞三连↓
版权声明
本文为[Datawhale]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Datawhale/article/details/124335517
边栏推荐
- Kubecost | kubernetes expenditure monitoring and management
- 26 years old, 0 basic software testing still in time
- Application of Beidou GPS satellite time synchronization device (satellite clock) in radio and television system
- PIP command and online and offline installation method
- Tencent build project image
- Leetcode (04)
- Mariadb互为主从(双主模式)配置
- Android 面试:事件分发8连问,安卓基础面试题
- With Feixiang, this task management artifact, the work is more convenient and efficient
- 软件架构设计师-创建型模式-单例模式-原型模式-工厂方法模式-抽象工厂模式-建造者模式
猜你喜欢

Get the sum of a value in each item object in the array (use of reduce)

Arcengine点,线,面,文本渲染
Android 92022-2022 byte beating Android interview real question analysis

3. fiddler证书安装和抓取hettps设置

Android 开发面试题集合整理(内含答案),【工作感悟】

It people should not only improve their earning ability, but also expand their ways of making money. The "immortal document" released by Tencent's technical officer is a hot network

博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA

dxg:TableView.FormatConditions 表格按条件高亮显示

知识就是力量,但更重要的是运用知识的能力---网页端微信扫码支付-技术设计

企业选择私有化部署的IM即时通讯软件,全力保护信息安全!
随机推荐
Borui data and F5 jointly build the full data chain DNA of financial technology from code to user
985硕艰难转行Android之路 加面经分享,美团安卓面试
Which domestic foreign exchange platforms are safe and formal?
Archengine custom tool mouse style settings
ArcGIS版本更新对比
arcengine线与面的相互转换
ArcGIS face gap inspection
MariaDB is configured as master-slave (dual master mode) to each other
Cloudera Manager HA模式搭建
企业选择私有化部署的IM即时通讯软件,全力保护信息安全!
Upload and download of shared folders on remote server
知识就是力量,但更重要的是运用知识的能力---网页端微信扫码支付-技术设计
北斗gps卫星时间同步装置(卫星时钟)在广电系统的应用
What happens when you run the NPM install command?
Principle analysis of ArcEngine print view and layout view
10天完成民猫电商毕设——用户模块实现(2nd day)
Apache iotdb's UDF Source Analysis (1)
每周问答精选:PolarDB-X完全兼容MySQL吗?
2020火爆全网系列:这是一份非常适合领取与收藏的Android进阶-面试重难点资料笔记!持续更新大厂高质量面试链接
Basic use and internal implementation principle of handerthread