機械学習のパラメータチューニングなど(ポエム感強し)


機械学習のパラメータチューニングをするには、
各パラメータのことを知らないといけない。
そのためには、その機械学習アルゴリズムをわかっていないといけない。

(当たり前なんだけど、パッケージに頼ると、スクラッチで書く人よりその辺りの意識が弱いと思う。)

 

探索は、for文などで行うことができる。
もしくは、sklearnのGridSearchCV()で行うことができる。 

ランダムサーチでサンプリングする方法もある。
ランダムサーチの実装方法としては、sklearnのRandomizedSearchCV()がある。

 

XGBoost,LightGBM,CatBoostなどは、sklearn傘下のものではないけれど、
GridSearchCV()、RandomizedSearchCV()は使える。
(sklearnの cross_validationがsklearn以外でも使えるようなものだ。)

 

使い方としては、以下参照。

sklearn.model_selection.GridSearchCV — scikit-learn 0.19.1 documentation

sklearn.model_selection.RandomizedSearchCV — scikit-learn 0.19.1 documentation

ポイントだと思うことを書く。

GridSearchCV()使うときは、GridSearchCV()のパラメータを使う(当たり前)。

RandomizedSearchCV()使うときは、RandomizedSearchCV()のパラメータを使う(当たり前)。

 じゃ、各アルゴリズム特有のパラメータは何処に入れるかというと、

GridSearchCV()のときは、GridSearchCV()のパラメータparam_grid に入れる(辞書型または、辞書のリスト)。

RandomizedSearchCV()のときは、RandomizedSearchCV()のパラメータparam_distributionsに入れる(辞書型)。

あとは、末尾にCVと付いてるように、交差検証もやれる。グリッドサーチもランダムサーチもcvというパラメータを持っている。特に指定しないと、3-fold CVになるらしい。

 

入れ子になった交差検証はこの辺を見ればいいと思う。

Nested versus non-nested cross-validation — scikit-learn 0.19.1 documentation

 

あとは、評価方法ですね。 

混同行列: TP(真陽性), TN(真陰性), FP(偽陽性), FN(偽陰性

精度 =  \displaystyle \frac{TP+TN}{TP+TN+FP+FN}

適合率 =  \displaystyle \frac{TP}{TP+FP}

再現率 =  \displaystyle \frac{TP}{TP+FN}

F値 = \displaystyle \frac{2\times適合率\times再現率}{   適合率+再現率   }         =    \displaystyle \frac{2TP}{2TP+FP+FN}

 


ROC

sklearn.metrics.roc_curve — scikit-learn 0.19.1 documentation

 

AUC

sklearn.metrics.auc — scikit-learn 0.19.1 documentation

 

<参考文献>

Python機械学習プログラミング 6章など。
Pythonではじめる機械学習 5章など。
https://www.slideshare.net/canard0328/ss-44288984

http://yag.xyz/blog/2015/08/08/xgboost-python/

https://qiita.com/Lewuathe/items/09d07d3ff366e0dd6b24
https://github.com/rasbt/python-machine-learning-book/blob/master/code/ch06/ch06.ipynb
https://github.com/amueller/introduction_to_ml_with_python/blob/master/05-model-evaluation-and-improvement.ipynb
https://www.kaggle.com/phunter/xgboost-with-gridsearchcv
https://qiita.com/ragAgar/items/2f6bebdba5f9d7381310
http://tkzs.hatenablog.com/entry/2015/10/06/015530
http://kamonohashiperry.com/archives/209
http://tekenuko.hatenablog.com/entry/2016/09/20/222453