高级集成学习技巧
Examined ensemble methods
- Averaging (or blending)
 - Weighted averaging
 - Conditional averaging
 - Bagging
 - Boosting
 - Stacking
 - StackNet
 
Averaging ensemble methods
举个例子,假设我们有一个名为 age 的变量,就像年龄一样,我们试着预测它。我们有两个模型:
- 低于 50,模型效果更好
 

- 高于 50,模型效果更好
 

那么如果我们试图结合它们将会发生什么呢?
Averaging(or blending)
- (model1 + model2) / 2
 

Weighted averaging
- (model1 x 0.7 + model 2 x 0.3)
 

看起来没有之前的好
Conditional averaging
- 各取好的部分
 

理想情况下,我们希望得到类似的结果
Bagging
Why Bagging
建模中有两个主要误差来源
- 1.由于偏差而存在误差(underfitting)
 - 2.由于方差而存在误差(overfitting)
 
通过略微不同的模型,确保预测不会有读取非常高的方差。这通常使它更具普遍性。
Parameters that control bagging?
- Changing the seed
 - Row(Sub) sampling or Bootstrapping
 - Shuffling
 - Column(Sub) sampling
 - Model-specific parameters
 - Number of models (or bags)
 - (Optionally) parallelism
 
###
Examples of bagging

##
##
Boosting
Boosting 是对每个模型构建的模型进行加权平均的一种形式,顺序地考虑以前的模型性能。
Weight based boosting

假设我们有一个表格数据集,有四个特征。 我们称它们为 x0,x1,x2 和 x3,我们希望使用这些功能来预测目标变量 y。
我们将预测值称为 pred,这些预测有一定的误差。我们可以计算这些绝对误差,|y - pred|。我们可以基于此生成一个新列或向量,在这里我们创建一个权重列,使用 1 加上绝对误差。当然有不同的方法来计算这个权重,现在我们只是以此为例。
所有接下来要做的是用这些特征去拟合新的模型,但每次也要增加这个权重列。这就是按顺序添加模型的方法。
Weight based boosting parameters
- Learning rate (or shrinkage or eta)
 - 每个模型只相信一点点:
predictionN = pred0*eta + pred1*eta + ... + predN*eta - Number of estimators
 - estimators 扩大一倍,eta 减小一倍
 - Input model - can be anything that accepts weights
 - Sub boosting type:
 - AdaBoost-Good implementation in sklearn(python)
 - LogitBoost-Good implementation in Weka(Java)
 
Residual based boosting [&]
我们使用同样的数据集做相同的事。预测出 pred 后
接下来会计算误差
将 error 作为新的 y 得到新的预测 new_pred
以 Rownum=1 为例:
最终预测=0.75 + 0.20 = 0.95 更接近于 1
这种方法很有效,可以很好的减小误差。
Residual based boosting parameters
- Learning rate (or shrinkage or eta)
 predictionN = pred0 + pred1*eta + ... + predN*eta- 前面的例子,如果 eta 为 0.1,则 Prediction=0.75 + 0.2*(0.1) = 0.77
 - Number of estimators
 - Row (sub)sampling
 - Column (sub)sampling
 - Input model - better be trees.
 - Sub boosting type:
 - Full gradient based
 - Dart
 
Residual based favourite implementations
- Xgboost
 - Lightgbm
 - H2O’s GBM
 - Catboost
 - Sklearn’s GBM
 
Stacking
Methodology
Wolpert in 1992 introduced stacking. It involves:
- Splitting the train set into two disjoint sets.
 
- Train several base learners on the first part.
 
- Make predictions with the base learners on the second (validation) part.
 
具体步骤
假设有 A,B,C 三个数据集,其中 A,B 的目标变量 y 已知。
然后
- 算法 0 拟合 A,预测 B 和 C,然后保存 pred0 到 B1,C1
 - 算法 1 拟合 A,预测 B 和 C,然后保存 pred1 到 B1,C1
 - 算法 2 拟合 A,预测 B 和 C,然后保存 pred2 到 B1,C1
 

- 算法 3 拟合 B1,预测 C1,得到最终结果 preds3
 
Stacking example
1  | from sklearn.ensemble import RandomForestRegressor  | 
Stacking(past) example

可以看到,它与我们使用Conditional averaging的结果非常近似。只是在 50 附件做的不够好,这是有道理的,因为模型没有见到目标变量,无法准确识别出 50 这个缺口。所以它只是尝试根据模型的输入来确定。
Things to be mindful of
- With time sensitive data - respect time
 - 如果你的数据带有时间元素,你需要指定你的 stacking,以便尊重时间。
 - Diversity as important as performance
 - 单一模型表现很重要,但模型的多样性也非常重要。当模型是坏的或弱的情况,你不需太担心,stacking 实际上可以从每个预测中提取到精华,得到好的结果。因此,你真正需要关注的是,我正在制作的模型能给我带来哪些信息,即使它通常很弱。
 - Diversity may come from:
 - Different algorithms
 - Different input features
 - Performance plateauing after N models
 - Meta model is normally modest
 
StackNet
https://github.com/kaz-Anova/StackNet
Ensembling Tips and Tricks
$1^{st}$ level tips
- Diversity based on algorithms:
 - 2-3 gradient boosted trees (lightgbm, xgboost, H2O, catboost)
 - 2-3 Neural nets (keras, pytorch)
 - 1-2 ExtraTrees/RandomForest (sklearn)
 - 1-2 linear models as in logistic/ridge regression, linear svm (sklearn)
 - 1-2 knn models (sklearn)
 - 1 Factorization machine (libfm)
 - 1 svm with nonlinear kernel(like RBF) if size/memory allows (sklearn)
 - Diversity based on input data:
 - Categorical features: One hot, label encoding, target encoding, likelihood encoding, frequency or counts
 - Numerical features: outliers, binning, derivatives, percentiles, scaling
 - Interactions: col1*/+-col2, groupby, unsupervised
 
$2^{st}$ level tips
- Simpler (or shallower) Algorithms:
 - gradient boosted trees with small depth(like 2 or 3)
 - Linear models with high regularization
 - Extra Trees (just don’t make them too big)
 - Shallow networks (as in 1 hidden layer, with not that many hidden neurons)
 - knn with BrayCurtis Distance
 - Brute forcing a search for best linear weights based on cv
 - Feature engineering:
 - pairwise differences between meta features
 - row-wise statistics like averages or stds
 - Standard feature selection techniques
 - For every 7.5 models in previous level we add 1 in meta (经验)
 - Be mindful to target leakage
 
Additional materials

本文作者 : HeoLis
原文链接 : https://ishero.net/%E9%AB%98%E7%BA%A7%E9%9B%86%E6%88%90%E5%AD%A6%E4%B9%A0%E6%8A%80%E5%B7%A7.html
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
学习、记录、分享、获得