3
3

More than 5 years have passed since last update.

[Kaggle]いろいろとTitanicしてみる:グリッドサーチ

Posted at

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

前回の続き

交差検証を調べていたら以下の記事に遭遇
https://qiita.com/tomov3/items/039d4271ed30490edf7b

これの後半にモデルに対するパラメータのチューニングの仕方が載っていたので、それを参考に今回は「ハイパーパラメータのチューニング」をやってみることにする。

ググる

https://blog.amedama.jp/entry/2017/09/05/221037
いくつかググってみたのだが、ここが自分には一番わかりやすかった。

つまるところ、それぞれのモデル?を作成する際のパラメータについて、
いっぱい試したいからそれを設定できるようにしたぜってことの様子。
なので、これだけ理解しても無駄だった。
それぞれのモデルに食わせたいパラメータを理解しないと意味がないと理解。

とりあえず決定木とSVMのパラメータを作ってみた

決定木

# GridSearchしてその結果最適なパラメータで設定
clf = DecisionTreeClassifier()
# 試行するパラメータを羅列する
params = {
        'max_depth': list(range(1, 10)),
        'min_samples_split' : list(range(3, 10)),
        'criterion': ['gini', 'entropy'],
    }
best_estimator = GridSearch(clf, params, 10, predictor_var, response_var)

SVM

clf = SVC(class_weight='balanced', random_state=0)
# 試行するパラメータを羅列する
params = {
        'C': [0.01, 0.1, 1.0],
        'gamma' : [0.01, 0.1, 1.0],
        'kernel': ['rbf', 'linear'],
    }
best_estimator = GridSearch(clf, params, 10, predictor_var, response_var)

GridSearchの中身そのものは一緒なので関数化してみた。

def GridSearch(clf, param_grid, cv_cnt, predictor_var, response_var):
    print(" --- grid_search ")
    # グリッドサーチにより最適値を求める
    # from sklearn import grid_search
    from sklearn.model_selection import GridSearchCV
    grid_search = GridSearchCV(estimator=clf, param_grid=param_grid, scoring='accuracy', cv=cv_cnt, n_jobs=-1)
    grid_search.fit(predictor_var, response_var)
    print(grid_search.best_score_)
    print(grid_search.best_params_)
    # print(gs.best_estimator_)

    # return grid_search.best_params_
    return grid_search.best_estimator_

best_estimatorを返却するよりはgrid_searchそのものを返却したほうが使いやすいのかな?
でも、スコアとかパラメタを返却されても使わないよな?
そしていずれのサイトにも書いてある「時間がかかる」という意味も理解。
ここら辺からPCのパワーが必要になってくるのかもしれない。

ついでに効果が高そうなRandomForestについてもGridSearchさせてみることにする
参考:http://blog.tatsushim.com/?p=63

clf = RandomForestClassifier()
params = {
        'n_estimators'      : [5, 10, 20, 30, 50, 100, 300],
        'max_features'      : [3, 5, 10, 15, 20],
        'random_state'      : [0],
        'n_jobs'            : [1],
        'min_samples_split' : [3, 5, 10, 15, 20, 25, 30, 40, 50, 100],
        'max_depth'         : [3, 5, 10, 15, 20, 25, 30, 40, 50, 100]
}
best_estimator = GridSearch(clf, params, 10, predictor_var, response_var)
return best_estimator

が、例外発生。
ValueError: max_features must be in (0, n_features]

max_featuresで怒られているのでいったんデフォルトにさせてそれ以外のパラメータで試行。
で、得られたパラメータが以下。
{'max_depth': 15, 'min_samples_split': 15, 'n_estimators': 50, 'n_jobs': 1, 'random_state': 0}
スコアは0.836139169473

このパラメータ+max_featuresで再実施。
が、やっぱり ValueError: max_features must be in (0, n_features]
何かが違っているんだろうな。
よくわからないので先に進むことにする。

アップロードしてみる

ということで、DecisionTree、SVM、Random Forestに対してGridSearchしたうえでパラメータを再設定しての結果が以下。

                model_name     score
6            Random Forest  0.836176
0             DecisionTree  0.823866
1       LogisticRegression  0.798048
2  Support Vector Machines  0.785587
4     Gaussian Naive Bayes  0.782329
3                k近傍法(KNN)  0.714028
5               Perceptron  0.552625

前回と比べるとまあまあポイントがアップしている。
なので、これでテストもかましてアップロードしてみることにする。

2018-09-23_16h32_11.png

だいぶ増えた。Rankingの方も真ん中ぐらいまで来た。

[参考]
https://qiita.com/yhyhyhjp/items/c81f7cea72a44a7bfd3a
https://qiita.com/cvusk/items/1f3b178f34c39beb29ff
https://qiita.com/koji-murakami/items/b7887f1cef11ddc443a4
http://neuro-educator.com/ml8/
http://starpentagon.net/analytics/scikit_learn_grid_search_cv/

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3