集成学习之Bagging/Boosting分类和回归

集成学习

0.Official Description

The goal of ensemble methods is to combine the predictions of several base estimators built with a given learning algorithm in order to improve generalizability / robustness over a single estimator.

Two families of ensemble methods are usually distinguished:

  • In averaging methods, the driving principle is to build several estimators independently and then to average their predictions. On average, the combined estimator is usually better than any of the single base estimator because its variance is reduced.

    Examples: Bagging methods, Forests of randomized trees, …

  • By contrast, in boosting methods, base estimators are built sequentially and one tries to reduce the bias of the combined estimator. The motivation is to combine several weak models to produce a powerful ensemble.

    Examples: AdaBoost, Gradient Tree Boosting, …

As they provide a way to reduce overfitting,

Bagging methods work best with strong and complex models (e.g., fully developed decision trees),

in contrast with Boosting methods which usually work best with weak models (e.g., shallow decision trees).

1.什么是集成学习
集成学习通过构建并结合多个学习器来完成学习任务,有时也被称为多分类器系统、基于委员会的学习等.集成学习通过将多个学习器进行结合,常可获得比单一学习器显著优越的泛化性能.
# 泛化能力(generalization ability)是指机器学习算法对新鲜样本的适应能力,,简而言之是在原有的数据集上添加新的数据集,通过训练输出一个合理的结果.学习的目的是学到隐含在数据背后的规律,对具有同一规律的学习集以外的数据,经过训练的网络也能给出合适的输出,该能力称为泛化能力.
2.集成学习分类
根据基学习器的生成方式,目前的集成学习方法大致可以分为两大类,
1. Bagging
		基学习器间不存在强依赖关系,可同时生成的并行化方法
2. Boosting
		基学习器间存在强依赖关系,必须串行生成的序列化方法
# 基学习器/基分类器/弱学习器 =====> 子训练集通过机器学习算法训练得到的模型
# 上述几个是同一个东西,叫法不同而已,统称weak learner,垃圾翻译常有,自求多福吧
3.结合策略

对于基学习器最终的结合策略常见的方法有如下几种:

  • 平均法

对于数值形输出,最常见的结合策略即为平均法:
H ( x ) = 1 T ∑ i = 1 T h i ( x ) H(x)=\frac{1}{T}\sum_{i=1}^{T}h_{i}(x) H(x)=T1i=1Thi(x)
其中
h i ( x ) 为 基 学 习 器 的 输 出 结 果 , H ( x ) 为 最 终 学 习 器 的 结 果 , T 为 基 学 习 器 的 个 数 . h_{i}(x)为基学习器的输出结果,H(x)为最终学习器的结果,T为基学习器的个数. hi(x)H(x)

  • 2
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
bagging算法和boosting算法都是集成学习算法,但它们的思路和实现方式有所不同。bagging算法(自举聚合法)是一种并行式的集成学习算法,它通过随机有放回地抽样来构造不同的子数据集,然后分别使用这些子数据集来训练不同的分类器,最终将多个分类器的结果进行平均或多数表决来预测新的数据。boosting算法(提升方法)是一种串行式的集成学习算法,它通过迭代地训练不同的分类器,每次训练的分类器都会关注前一次分类器的错误,并试图修正这些错误,最终将多个分类器的结果加权求和来预测新的数据。 下面我们使用Python的scikit-learn库来比较bagging算法和boosting算法在数据集上的表现: ``` ## 导入需要的库 from sklearn.ensemble import GradientBoostingRegressor, BaggingRegressor from sklearn.metrics import mean_squared_error from sklearn.datasets import make_regression from sklearn.model_selection import train_test_split ## 生成数据集 X, y = make_regression(n_samples=1000, n_features=10, noise=0.1, random_state=42) ## 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) ## 训练Gradient Boosting模型 gb_model = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, random_state=42) gb_model.fit(X_train, y_train) ## 预测测试集 gb_pred = gb_model.predict(X_test) ## 计算Gradient Boosting模型的均方误差 gb_mse = mean_squared_error(y_test, gb_pred) print("Gradient Boosting MSE:", gb_mse) ## 训练Bagging模型 dt_model = DecisionTreeRegressor() bg_model = BaggingRegressor(base_estimator=dt_model, n_estimators=100, random_state=42) bg_model.fit(X_train, y_train) ## 预测测试集 bg_pred = bg_model.predict(X_test) ## 计算Bagging模型的均方误差 bg_mse = mean_squared_error(y_test, bg_pred) print("Bagging MSE:", bg_mse) ``` 以上代码中,我们首先生成了一个具有10个特征的回归数据集,并划分训练集和测试集。然后,我们使用GradientBoostingRegressor和BaggingRegressor来分别训练Gradient Boosting模型和Bagging模型。最后,我们使用均方误差(MSE)来评估两个模型的预测效果。 需要注意的是,这里的数据集是人工生成的,因此MSE的值仅供参考,实际应用中会受到数据集的特点、模型参数、评价指标等多种因素的影响。通常来说,在不同的数据集和应用场景下,bagging算法和boosting算法的表现也会有所不同。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值