CART のハイパーパラメータのチューニング

Pythonで学ぶ木ベースのMachine Learning

Elie Kawerk

Data Scientist

ハイパーパラメータ

機械学習モデル:

  • パラメータ: データから学習

    • CART 例: ノードの分割点、分割特徴 など
  • ハイパーパラメータ: データからは学習せず、学習前に設定

    • CART 例: max_depthmin_samples_leaf、分割基準 など
Pythonで学ぶ木ベースのMachine Learning

ハイパーパラメータチューニングとは

  • 問題: 学習アルゴリズムの最適ハイパーパラメータを探索する。

  • 解決策: 最適なモデルとなるハイパーパラメータ集合を見つける。

  • 最適モデル: 最良のスコアを出す。

  • スコア: sklearn では既定で accuracy(分類)、$R^2$(回帰)。

  • 汎化性能の推定に交差検証を用いる。

Pythonで学ぶ木ベースのMachine Learning

なぜチューニングするか

  • sklearn の既定ハイパーパラメータは問題ごとに最適とは限らない。

  • 最良の性能のためにハイパーパラメータを調整する必要がある。

Pythonで学ぶ木ベースのMachine Learning

ハイパーパラメータチューニングの手法

  • グリッドサーチ

  • ランダムサーチ

  • ベイズ最適化

  • 遺伝的アルゴリズム

  • ...

Pythonで学ぶ木ベースのMachine Learning

グリッドサーチ交差検証

  • 離散的なハイパーパラメータ値のグリッドを手動で設定。

  • モデル性能の指標を設定。

  • グリッドを総当たりで探索。

  • 各ハイパーパラメータ集合ごとに、各モデルの CV スコアを評価。

  • 最良の CV スコアを出したモデルのハイパーパラメータが最適。

Pythonで学ぶ木ベースのMachine Learning

グリッドサーチ交差検証: 例

  • ハイパーパラメータのグリッド:
    • max_depth = {2,3,4},
    • min_samples_leaf = {0.05, 0.1}
  • ハイパーパラメータ空間 = { (2,0.05) , (2,0.1) , (3,0.05), ... }
  • CV スコア = { $score_{(2,0.05)}$ , ... }
  • 最適ハイパーパラメータ = 最高の CV スコアに対応する組。
Pythonで学ぶ木ベースのMachine Learning

sklearn における CART のハイパーパラメータの確認

# Import DecisionTreeClassifier
from sklearn.tree import DecisionTreeClassifier

# Set seed to 1 for reproducibility
SEED = 1

# Instantiate a DecisionTreeClassifier 'dt'
dt = DecisionTreeClassifier(random_state=SEED)

Pythonで学ぶ木ベースのMachine Learning

sklearn における CART のハイパーパラメータの確認

# Print out 'dt's hyperparameters
print(dt.get_params())
        {'class_weight': None,
         'criterion': 'gini',
         'max_depth': None,
         'max_features': None,
         'max_leaf_nodes': None,
         'min_impurity_decrease': 0.0,
         'min_impurity_split': None,
         'min_samples_leaf': 1,
         'min_samples_split': 2,
         'min_weight_fraction_leaf': 0.0,
         'presort': False,
         'random_state': 1,
         'splitter': 'best'}
Pythonで学ぶ木ベースのMachine Learning
# Import GridSearchCV
from sklearn.model_selection import GridSearchCV

# Define the grid of hyperparameters 'params_dt' params_dt = { 'max_depth': [3, 4,5, 6], 'min_samples_leaf': [0.04, 0.06, 0.08], 'max_features': [0.2, 0.4,0.6, 0.8] }
# Instantiate a 10-fold CV grid search object 'grid_dt' grid_dt = GridSearchCV(estimator=dt, param_grid=params_dt, scoring='accuracy', cv=10, n_jobs=-1)
# Fit 'grid_dt' to the training data grid_dt.fit(X_train, y_train)
Pythonで学ぶ木ベースのMachine Learning

最良ハイパーパラメータの取得

# Extract best hyperparameters from 'grid_dt'
best_hyperparams = grid_dt.best_params_
print('Best hyerparameters:\n', best_hyperparams)
Best hyerparameters:
  {'max_depth': 3, 'max_features': 0.4, 'min_samples_leaf': 0.06}
# Extract best CV score from 'grid_dt'
best_CV_score = grid_dt.best_score_
print('Best CV accuracy'.format(best_CV_score))
Best CV accuracy: 0.938
Pythonで学ぶ木ベースのMachine Learning

最良推定器の取得

# Extract best model from 'grid_dt'
best_model = grid_dt.best_estimator_

# Evaluate test set accuracy test_acc = best_model.score(X_test,y_test) # Print test set accuracy print("Test set accuracy of best model: {:.3f}".format(test_acc))
Test set accuracy of best model: 0.947
Pythonで学ぶ木ベースのMachine Learning

練習に進みましょう!

Pythonで学ぶ木ベースのMachine Learning

Preparing Video For Download...