【2021.04--集成学习(中)-Task08】bagging介绍

本次 DataWhale 第二十三期组队学习,其开源内容的链接为:https://github.com/datawhalechina/team-learning-data-mining/tree/master/EnsembleLearning

首先我们理解下boostrap,这是一种抽样思想,他的核心思路如下图:
在这里插入图片描述

bootstrap在小样本时效果很好,可以通过自身从重抽样估计真实分布。在bootstrap的基础上,在弱学习器的“准确性”和“多样性”上进行有侧重的关注:个体学习器间存在强依赖关系、必须串行生成的序列化方法与个体学习器间不存在强依赖关系、可同时生成的并行化方法。前者的代表是boosting,后者就是今天组队学习的内容——bagging。

Bagging时并行式集成学习方式的代表。通过n次随机采样操作,得到的训练集训练出n个基学习器,在根据一定的规则,如投票,得到最终结果。Bagging是一个高效的集成学习算法,且在偏差-方差的角度上,主要关注降低方差。下面是在一个bagging的实例。

# 构建数据集
from sklearn.datasets import make_classification

X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=5)

print(X.shape, y.shape)
(1000, 20) (1000,)
import numpy as np
from sklearn.model_selection import cross_val_score, RepeatedStratifiedKFold
from sklearn.ensemble import BaggingClassifier

# 定义模型
model = BaggingClassifier()

# 划分验证集
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, error_score = 'raise')

print('Accuracy: %.3f (%.3f)' % (np.mean(n_scores), np.std(n_scores)))
Accuracy: 0.862 (0.041)
# 随机森林
from sklearn.model_selection import cross_val_score, RepeatedStratifiedKFold
from sklearn.ensemble import RandomForestClassifier

# 使用默认参数
rfc =  RandomForestClassifier()
cv = RepeatedStratifiedKFold(n_splits = 10, n_repeats = 3, random_state = 1)
n_scores = cross_val_score(rfc, X, y, scoring = 'accuracy', cv = cv, n_jobs = -1, error_score = 'raise')

print('Accuracy: %.3f (%.3f)' % (np.mean(n_scores), np.std(n_scores)))
Accuracy: 0.893 (0.039)

同时参考链接:总结:Bootstrap(自助法),Bagging,Boosting(提升)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值