アンサンブル手法

Pythonで学ぶMachine Learning面接対策

Lisa Stuart

Data Scientist

アンサンブル学習の技法

  • Bootstrap Aggregation(バギング)
  • ブースティング
  • モデルスタッキング
Pythonで学ぶMachine Learning面接対策

誤差の測定

Pythonで学ぶMachine Learning面接対策

浅い木

Pythonで学ぶMachine Learning面接対策

深い木

Pythonで学ぶMachine Learning面接対策

太い木

Pythonで学ぶMachine Learning面接対策

線形モデル

Pythonで学ぶMachine Learning面接対策

バイアス

線形関係の仮定(誤り)

  • バイアス大
  • 過小適合
  • 汎化性能が低い
  • 複雑さを上げるとバイアス低下
Pythonで学ぶMachine Learning面接対策

複雑なモデル

Pythonで学ぶMachine Learning面接対策

分散

高複雑度モデル:

  • 分散大
  • 過学習
  • 汎化性能が低い
Pythonで学ぶMachine Learning面接対策

バイアス–バリアンストレードオフ

1 出典: Trevor Hastie、Robert Tibshirani、Jerome Friedman『Elements of Statistical Learning』
Pythonで学ぶMachine Learning面接対策

バギング(ブートストラップ集約)

  • ブートストラップ標本
    • 復元抽出のサブセット
    • 同じ行が選ばれることがある
  • 標本ごとにモデルを作成
  • 出力を平均化
  • 分散を低減

1 https://medium.com/@rrfd/boosting-bagging-and-stacking-ensemble-methods-with-sklearn-and-mlens-a455c0c982de
Pythonで学ぶMachine Learning面接対策

ブースティング

  • 複数モデルを逐次的に構築
  • 誤分類に重み付け
  • バイアスを低減

1 https://blog.bigml.com/2017/03/14/introduction-to-boosted-trees/
Pythonで学ぶMachine Learning面接対策

モデルスタッキング

  • モデル1の予測
  • モデル2の予測…
  • モデルNの予測
  • 最高精度になるようスタック
    • ベースモデル(モデルN)の予測を第2層モデルの入力に利用

1 http://supunsetunga.blogspot.com/
Pythonで学ぶMachine Learning面接対策

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で学ぶMachine Learning面接対策

アンサンブル関数

アルゴリズム 関数
ブートストラップ集約 sklearn.ensemble.BaggingClassifier()
ブースティング sklearn.ensemble.AdaBoostClassifier()
XGBoost xgboost.XGBClassifier()
Pythonで学ぶMachine Learning面接対策

バギング vs ブースティング

手法 バイアス 分散
Bootstrap aggregation(バギング) 増加 減少
ブースティング 減少 増加
Pythonで学ぶMachine Learning面接対策

主要アンサンブル手法:択一問題

機械学習の主要なアンサンブル手法について正しい記述はどれですか。正しいものを選んでください。

  • ブースティングはモデル分散を下げる。
  • ブースティングは分類器の予測力を高める。
  • バギングはモデルのバイアスを下げる。
  • モデルスタッキングは各モデルの予測を組み合わせ、高精度のモデルを作る。
Pythonで学ぶMachine Learning面接対策

主要アンサンブル手法:解答

機械学習の主要なアンサンブル手法について正しい記述はどれですか。正解は次のとおりです。

  • モデルスタッキングは各モデルの予測を組み合わせ、高精度のモデルを作る。(複数モデルの予測から得た最終モデルは、個々のモデルより概ね優れます。)
Pythonで学ぶMachine Learning面接対策

主要アンサンブル手法:誤答の解説

機械学習の主要なアンサンブル手法について正しい記述はどれですか。

  • ブースティングはモデル分散を下げる。(ブースティングはバイアスを下げ、その過程で分散を高めて最適な汎化を探ります。)
  • ブースティングは分類器の予測力を高める。(ブースティングはバイアスを下げますが、予測力が必ず向上するとは限りません。)
  • バギングはモデルのバイアスを下げる。(バギングは分散を下げます。)
Pythonで学ぶMachine Learning面接対策

Ayo berlatih!

Pythonで学ぶMachine Learning面接対策

Preparing Video For Download...