集成方法

用 Python 练习机器学习面试题

Lisa Stuart

Data Scientist

集成学习技术

  • 自助聚合(Bagging)
  • 提升(Boosting)
  • 模型堆叠(Stacking)
用 Python 练习机器学习面试题

误差度量

用 Python 练习机器学习面试题

浅层树

用 Python 练习机器学习面试题

深层树

用 Python 练习机器学习面试题

宽树

用 Python 练习机器学习面试题

线性模型

用 Python 练习机器学习面试题

偏差(Bias)

线性关系假设(错误)

  • 高偏差
  • 欠拟合
  • 泛化能力差
  • 提高复杂度可降低偏差
用 Python 练习机器学习面试题

复杂模型

用 Python 练习机器学习面试题

方差(Variance)

高复杂度模型:

  • 高方差
  • 过拟合
  • 泛化能力差
用 Python 练习机器学习面试题

偏差-方差权衡

1 来源:Trevor Hastie、Robert Tibshirani、Jerome Friedman 著《统计学习要素》
用 Python 练习机器学习面试题

Bagging(自助聚合)

  • 自助样本
    • 有放回抽样子集
    • 同一行可被多次选中
  • 为每个样本建模
  • 对输出取平均
  • 降低方差

1 https://medium.com/@rrfd/boosting-bagging-and-stacking-ensemble-methods-with-sklearn-and-mlens-a455c0c982de
用 Python 练习机器学习面试题

提升(Boosting)

  • 多个模型按序训练
  • 加大错误预测的权重
  • 降低偏差

1 https://blog.bigml.com/2017/03/14/introduction-to-boosted-trees/
用 Python 练习机器学习面试题

模型堆叠

  • 模型1的预测
  • 模型2的预测……
  • 模型N的预测
  • 堆叠以获得更高准确率的模型
    • 用基础模型(模型N)的预测作为二级模型的输入

1 http://supunsetunga.blogspot.com/
用 Python 练习机器学习面试题

Vecstack 包

# import modules
from sklearn.ensemble import BaggingClassifier
from sklearn.ensemble import AdaBoostClassifier
from xgboost import XGBClassifier
from vecstack import stacking

# Create list: stacked_models
stacked_models = [BaggingClassifier(n_estimators=25, random_state=123), AdaBoostClassifier(n_estimators=25, random_state=123)]

# Stack the models: stack_train, stack_test
stack_train, stack_test = stacking(stacked_models, X_train, y_train, X_test, regression=False, mode='oof_pred_bag', 
                                   needs_proba=False, metric=accuracy_score, n_folds=4, stratified=True, shuffle=True, random_state=0, verbose=2)

# Initialize and fit 2nd level model
final_model = XGBClassifier(random_state=123, n_jobs=-1, learning_rate=0.1, n_estimators=10, max_depth=3)
final_model_fit = final_model.fit(stack_train, y_train)

# Predict: stacked_pred
stacked_pred = final_model.predict(stack_test)

# Final prediction score
print('Final prediction score: [%.8f]' % accuracy_score(y_test, stacked_pred))
1 https://towardsdatascience.com/automate-stacking-in-python-fc3e7834772e
用 Python 练习机器学习面试题

集成函数

算法 函数
自助聚合 sklearn.ensemble.BaggingClassifier()
提升 sklearn.ensemble.AdaBoostClassifier()
XGBoost xgboost.XGBClassifier()
用 Python 练习机器学习面试题

Bagging 与 Boosting 对比

技术 偏差 方差
自助聚合(Bagging) 增加 降低
提升(Boosting) 降低 增加
用 Python 练习机器学习面试题

主要集成技术 单选题

关于机器学习中三大集成技术,下列哪项表述正确?请选择正确的一项:

  • 提升方法会降低模型方差。
  • 提升方法会提高分类器的预测能力。
  • 自助聚合(Bagging)会降低模型偏差。
  • 模型堆叠将各个模型的预测组合,得到更高准确率的模型。
用 Python 练习机器学习面试题

主要集成技术 单选题:答案

关于机器学习中三大集成技术的判断题,正确答案是:

  • 模型堆叠将各个模型的预测组合,得到更高准确率的模型。(由多个模型预测构成的最终模型几乎总能优于单个模型。)
用 Python 练习机器学习面试题

主要集成技术 单选题:错误选项

关于机器学习中三大集成技术,下列说法为:

  • 提升方法会降低模型方差。(提升主要降低偏差,同时可能提高方差,以寻求最佳泛化的平衡。)
  • 提升方法会提高分类器的预测能力。(提升降低偏差,是否提高预测能力不一定。)
  • 自助聚合(Bagging)会降低模型偏差。(Bagging 降低的是方差。)
用 Python 练习机器学习面试题

Vamos praticar!

用 Python 练习机器学习面试题

Preparing Video For Download...