天天看点

集成学习之Bagging/Boosting分类和回归集成学习scikit-learn中集成学习的实现

集成学习

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)=T1​i=1∑T​hi​(x)

其中

h i ( x ) 为 基 学 习 器 的 输 出 结 果 , H ( x ) 为 最 终 学 习 器 的 结 果 , T 为 基 学 习 器 的 个 数 . h_{i}(x)为基学习器的输出结果,H(x)为最终学习器的结果,T为基学习器的个数. hi​(x)为基学习器的输出结果,H(x)为最终学习器的结果,T为基学习器的个数.

  • 加权平均法

H ( x ) = ∑ i = 1 T w i h i ( x ) H(x)=\sum_{i=1}^{T}w_{i}h_{i}(x) H(x)=i=1∑T​wi​hi​(x)

其 中 w i 是 基 学 习 器 h i 的 权 重 , 通 常 要 求 w i ⩾ 0 , ∑ i = 1 T w i = 1. 显 然 , 简 单 平 均 法 是 加 权 平 法 , 使 w i = 1 / T 的 特 例 其中w_{i}是基学习器h_{i}的权重,通常要求w_{i}\geqslant 0,\sum_{i=1}^{T}w_{i}=1.显然,简单平均法是加权平法,使w_{i}=1/T的特例 其中wi​是基学习器hi​的权重,通常要求wi​⩾0,i=1∑T​wi​=1.显然,简单平均法是加权平法,使wi​=1/T的特例

  • 投票法

预测结果为得票最多的标记,若同时又多个标记获得相同的票数,则从中随机选择一个.

  • 学习法

当训练数据很多时,可以通过另一个学习器来对所有基学习器产生结果的结合方法进行学习,这时候个体学习器称为初级学习器,用于结合的学习器成为次级学习器或元学习器.

4.Bagging

1. Bagging算法(Bootstrap aggregating,引导聚集算法)又称装袋算法,是机器学习领域的一种团体学习算法.Bagging算法可与其他分类,回归算法结合,提高其准确率,稳定性的同时,通过降低结果的方差,避免过拟合的发生.
           
2. 算法步骤
        1.给定一个大小为n的训练集D,Bagging算法从中均匀,有放回地选出m个大小为n'的子集Di,作为新的训练集.
# m个子训练集之间是相互独立的!!!!!
        2.在这m个训练集上使用分类,回归等算法,则可得到m个模型(弱学习器/基分类器).
        3.再通过取平均值,取多数票等方法,即可得到Bagging的结果.
# 对于分类问题,上述m个模型采用投票的方式得到分类结果;对于回归问题,计算上述m个模型的均值作为结果.
3. 具体解释
# Bagging以并行方式生成基学习器
Bagging机制:我们采样出T个含m个样本的采样集.然后基于每个采样集训练出一个学习器,再将学习器进行结合.对分类任务使用投票法,对回归任务采用平均值法.

从偏差-方差的角度,Bagging主要关注降低方差,因此它在容易受到样本扰动的学习器(如不剪枝的决策树、神经网络)中效果更明显.
           
集成学习之Bagging/Boosting分类和回归集成学习scikit-learn中集成学习的实现

5.Boosting

1. Boosting是一组可将弱学习器提升为强学习器的算法,这族算法的工作机制类似:先从初始训练集训练出一个基学习器,再根据基学习器的表现对训练样本分布进行调整,使得先前基学习器错误预测的训练样本在后续得到更大权重,然后基于调整后的样本分布来训练下一个基学习器;如此重复进行;直至基学习器数目达到事先指定的值T;最终将这T个基学习器进行加权结合.
2. 子训练集生成
		没有先验知识的情况下,初始的分布应为等概分布,也就是训练集如果有n个样本,每个样本的权重为1/n.每次循环后提高错误样本的权重,被错误预测的样本在训练集中所占权重增大,使得下一次循环的基分类器,能够集中力量对这些错误样本进行判断.
#  those training examples that were incorrectly predicted by the boosted model induced at the previous step have their weights increased, whereas the weights are decreased for those that were predicted correctly.As iterations proceed, examples that are difficult to predict receive ever-increasing influence. Each subsequent weak learner is thereby forced to concentrate on the examples that are missed by the previous ones in the sequence.
3. 具体解释
# Boosting以串行方式生成基学习器
Boosting算法会为每个训练样本赋予一个权值.第一轮每个训练样本的权重都一样.每次用训练完的新分类器标注各个样本,若某个样本点已被分类正确,则将其权值降低,并以该权重进行下一次数据的抽样(抽中的概率减小);若样本点未被正确分类,则提高其权值,并以该权重进行下一次数据的抽样(抽中的概率增大).权值越高的样本在下一次训练中所占的比重越大,也就是说越难区分的样本在训练过程中会变得越来越重要.整个迭代过程直到错误率足够小或达到一定次数才停止. 

从偏差-方差的角度,Boosting主要关注降低偏差,所以Boosting基于泛化性能很弱的学习器能够造出很强的集成.
           
集成学习之Bagging/Boosting分类和回归集成学习scikit-learn中集成学习的实现

6.Bagging/Boosting的区别

1. 子训练集选择
Bagging:采用有放回抽样的方式,生成m个子训练集.
Boosting:Boosting算法会为每一个训练样本赋予一个权重值,每一轮训练集的样本不变,只是将训练集中每个样本的权重根据上一轮的训练结果进行权重的调整.
2. 样本权重
Bagging:由于是有放回的抽样,每个样本的权重值相同
Boosting:每一轮会根据上一轮错误率调整样本的权重值,错误率越大,权重值越大
3. 基学习器的生成
Bagging:并行生成
Boosting:串行生成
4. 基学习器权重
Bagging:每个基学习器权重相等
Boosting:每个基学习器有相应的权重,对于训练误差小的基学习器有更大的权重值
5. 偏差和方差
Bagging:Bagging主要关注降低方差(variance)
Boosting:Boosting主要关注降低偏差(bias)
           

7.方差和偏差

1. 机器学习中,我们使用机器学习算法对数据集进行训练得到相应的模型,通常的做法是定义一个误差函数(Loss Function),通过将误差函数最小化的过程,来提高模型的性能(Performance),而我们训练一个模型的目的是为了解决实际的问题(即泛化),单纯将数据集的误差函数最小化,并不能保证模型在泛化过程中是最优的,甚至不能保证模型是可用的.
		泛化误差:模型在测试集上的误差
		经验误差:模型在训练集上的误差
2. 偏差(Bias)和方差(Variance)是针对泛化(Generalization)来说的.
		Generalization Error = Bias + Variance + Noise
3. 误差,方差,偏差,协方差,噪声
		误差:整个模型的准确度,以真实值为标准.
		方差:是模型每一次输出结果与模型输出期望之间的误差,即模型的稳定性.描述数据离散程度,数据波动性,会影响模型的预测结果.
		偏差:是模型在样本上的输出与真实值之间的误差,即模型本身的精准度.
		噪声:任何学习算法在泛化能力的下界,和预测结果不一致的数据点,但与最终结果不一致,无法通过模型降低.
		协方差:衡量两个变量的总体误差.而方差是协方差的一种特殊情况,即当两个变量是相同的情况.
           
左上:低偏差,低方差.预测准确到高,模型很稳定,数据集中.(理想模型)
右上:低偏差,高方差.预测结果较准确,模型不稳定,数据波动影响预测结果,数据分散.
左下:高偏差,低方差.预测结果不准确,模型稳定,数据集中.
右下:高方差,高偏差.预测结果不准确,模型不稳定,数据分散.
4. 欠拟合,过拟合与方差,偏差区别
欠拟合:高偏差,低方差,模型在训练集和测试集表现都不好的情况
过拟合:低偏差,高方差,模型在训练集上表现优秀(含噪声),在测试集上不精确
           
名称 欠拟合 过拟合
方差( 训练数据)
偏差(训练数据)
方差(测试数据)
偏差(测试数据)
5. 方差,偏差与模型复杂度的关系
模型复杂度越大(参数多),偏差越小,方差越大 ====> 易出现过拟合
模型复杂度越小(参数少),方差越小,偏差越大 ====> 易出现欠拟合
           

scikit-learn中集成学习的实现

1.Bagging

在scikit-learn中,使用BaggingClassifier/Baggingregressor实现Bagging的分类和回归.
           
sklearn.ensemble中Bagging分类/回归
class sklearn.ensemble.BaggingClassifier(base_estimator=None, 
                        				n_estimators=10, 
                        				max_samples=1.0, 
                        				max_features=1.0, 
                        				bootstrap=True, 
                        				bootstrap_features=False, 
                        				oob_score=False, 
                        				warm_start=False, 
                        				n_jobs=None, 
                        				random_state=None, 
                        				verbose=0)
# 参数Parameters
base_estimator: 用于训练每个子训练集的机器学习算法, default=None,若为None,是decision tree
n_estimators: 生成基学习器(模型弱学习器)的数量, default=10
# 我们的使用的数据,每一行是一个样本
max_samples: 子训练集样本数量, default=1.0
max_features: 子训练集特征数量, default=1.0
bootstrap: 子训练集是否有放回的生成, default=True
bootstrap_features: 子训练集特征是否有放回的生成, default=False
oob_score: 使用out of bag数据评估泛化误差, default=False
warm_start: default=False,为 True 复用上一次调用的solution去训练数据集生成模型,并且生成更多的基学习器/模型;
为 False 进行一次新的集成学习
n_jobs: 并行运行fit和predict的job数量,default=None,若为-1,job的数量被设定为核的数量
random_state: default=None,随机种子数,为了保证程序每次运行都分割一样的训练集和测试集,保证每次随机的结果都是一样的.在需要设置random_state的地方给其赋一个值,当多次运行此段代码能够得到完全一样的结果,别人运行此代码也可以复现你的过程.若不设置此参数则会随机选择一个种子,执行结果也会因此而不同了.虽然可以对random_state进行调参,但是调参后在训练集上表现好的模型未必在陌生训练集上表现好,所以一般会随便选取一个random_state的值作为参数.
    其取值不变时,用相同的训练集建树得到的结果一模一样,对测试集的预测结果也是一样的;
    其值改变时,得到的结果不同;
    若不设置此参数,则函数会自动选择一种随机模式,每次得到的结果也就不同.
verbose: 控制fit和predict过程中的冗余度,default=None
# 属性Attributes
base_estimator_ : estimator
The base estimator from which the ensemble is grown.

estimators_ : list of estimators
The collection of fitted base estimators.

estimators_samples_ : list of arrays
The subset of drawn samples (i.e., the in-bag samples) for each base estimator. Each subset is defined by a boolean mask.

estimators_features_ : list of arrays
The subset of drawn features for each base estimator.

classes_ : array of shape = [n_classes]
The classes labels.

n_classes_ : int or list
The number of classes.

oob_score_ : float
Score of the training dataset obtained using an out-of-bag estimate.

oob_decision_function_ : array of shape = [n_samples, n_classes]
Decision function computed with out-of-bag estimate on the training	set. If n_estimators is small it might be possible that a data point was never left out during the bootstrap. In this case,`oob_decision_function_` might contain NaN.
           
class sklearn.ensemble.BaggingRegressor(base_estimator=None, 
                                        n_estimators=10, 
                                        max_samples=1.0, 
                                        max_features=1.0, 
                                        bootstrap=True, 
                                        bootstrap_features=False, 
                                        oob_score=False, 
                                        warm_start=False, 
                                        n_jobs=None, 
                                        random_state=None, 
                                        verbose=0)
# 参数Parameters
含义同BaggingClassifier
# 属性Attributes
oob_prediction_ : array of shape = [n_samples]
Prediction computed with out-of-bag estimate on the training set. If n_estimators is small it might be possible that a data point was never left out during the bootstrap. In this case, oob_prediction_ might contain NaN.
其余同BaggingClassifier
           

2.Boosting

Adaboost

class sklearn.ensemble.AdaBoostClassifier(base_estimator=None, 
                                          n_estimators=50, 
                                          learning_rate=1.0, 
                                          algorithm=’SAMME.R’, 
                                          random_state=None)
# 参数Parameters
base_estimator: 用于训练基学习器的机器学习算法,要求base_estimator支持样本赋权(sample weighting),拥有合适的classes_,n_classes_属性.default=None, 意味着base_estimator为DecisionTreeClassifier(max_depth=1)
n_estimators : 串行生成的基学习器的最大数量,在完美的训练过程中,训练过程会早于这个数字表示的次数停止,default=50
learning_rate: 用于缩小各个基学习器的贡献,需要在learning_rate和n_estimators之间进行权衡,折衷,default=1
algrithom:

algorithm : default=’SAMME.R’,{‘SAMME’, ‘SAMME.R’}
If ‘SAMME.R’ then use the SAMME.R real boosting algorithm. base_estimator must support calculation of class probabilities. If ‘SAMME’ then use the SAMME discrete boosting algorithm. The SAMME.R algorithm typically converges faster than SAMME, achieving a lower test error with fewer boosting iterations.

# 其余同上

# 属性Attributes
estimators_ : list of classifiers
The collection of fitted sub-estimators.

classes_ : array of shape = [n_classes]
The classes labels.

n_classes_ : int
The number of classes.

estimator_weights_ : array of floats
Weights for each estimator in the boosted ensemble.

estimator_errors_ : array of floats
Classification error for each estimator in the boosted ensemble.

feature_importances_ : array of shape = [n_features]
Return the feature importances (the higher, the more important the feature).
           
class sklearn.ensemble.AdaBoostRegressor(base_estimator=None, 
                                         n_estimators=50, 
                                         learning_rate=1.0, 
                                         loss=’linear’, 
                                         random_state=None)
# 参数Parameter
base_estimator:
用于训练基学习器的机器学习算法,要求base_estimator支持样本赋权(sample weighting),拥有合适的classes_,n_classes_属性.default=None, 意味着base_estimator为DecisionTreeRegressor(max_depth=3)
loss:
每次boosting迭代之后,当调整权重时,使用的损失函数, {'linear','square','exponential'},default='linear'

# 属性Attributes
estimators_ : list of classifiers
The collection of fitted sub-estimators.

estimator_weights_ : array of floats
Weights for each estimator in the boosted ensemble.

estimator_errors_ : array of floats
Regression error for each estimator in the boosted ensemble.

feature_importances_ : array of shape = [n_features]
Return the feature importances (the higher, the more important the feature).
           

GradientBoosting

未完待续

@Copyright By 扫地小僧SWM

继续阅读