Pythonで学ぶ木ベースのMachine Learning
Elie Kawerk
Data Scientist
機械学習モデル:
パラメータ: データから学習
ハイパーパラメータ: データからは学習せず、学習前に設定
max_depth、min_samples_leaf、分割基準 など問題: 学習アルゴリズムの最適ハイパーパラメータを探索する。
解決策: 最適なモデルとなるハイパーパラメータ集合を見つける。
最適モデル: 最良のスコアを出す。
スコア: sklearn では既定で accuracy(分類)、$R^2$(回帰)。
汎化性能の推定に交差検証を用いる。
sklearn の既定ハイパーパラメータは問題ごとに最適とは限らない。
最良の性能のためにハイパーパラメータを調整する必要がある。
グリッドサーチ
ランダムサーチ
ベイズ最適化
遺伝的アルゴリズム
...
離散的なハイパーパラメータ値のグリッドを手動で設定。
モデル性能の指標を設定。
グリッドを総当たりで探索。
各ハイパーパラメータ集合ごとに、各モデルの CV スコアを評価。
最良の CV スコアを出したモデルのハイパーパラメータが最適。
max_depth = {2,3,4},min_samples_leaf = {0.05, 0.1}# Import DecisionTreeClassifier
from sklearn.tree import DecisionTreeClassifier
# Set seed to 1 for reproducibility
SEED = 1
# Instantiate a DecisionTreeClassifier 'dt'
dt = DecisionTreeClassifier(random_state=SEED)
# 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'}
# 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)
# 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
# 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