知識不要で機械学習!?Pycaretでモデルのチューニング

この記事で扱う内容

前回書いた記事の続きになります!

前回はpycaretの学習・推論までを書かせていただきました!

今回は可視化・モデルのチューニングを行います!

実行環境

前回と変更なしです!

Google Colaboratory

Pycaret 2.0

モデルのチューニング

前回の記事で作成したモデルのチューニングを行います。

(ハイパーパラメータのチューニングを行う)

# 前回の記事で紹介したcreate_modelでモデルを作成する
model = create_model("lightgbm")

# チューニング
model_tune = tune_model(model)

精度がどのくらいよくなったのか見ます。

predict = model.predict(exp1[3])
from sklearn import metrics
precision, recall, thresholds = metrics.precision_recall_curve(exp1[5], predict)
auc = metrics.auc(recall, precision)
print(auc)

# ↑の実行結果(精度低い・・・)
0.5891415389899795

# チューニング後
predict_tune = model_tune.predict(exp1[3])
precision, recall, thresholds = metrics.precision_recall_curve(exp1[5], predict_tune)
auc_tune = metrics.auc(recall, precision)
print(auc_tune)
# ↑の実行結果(精度up)
0.5998562590191492

ハイパーパラメータの差異を確認(結構変わっている)

スクリーンショット 2020-09-20 23.14.40

ちなみにtune_modelにcustom_gridオプションを指定すると、任意のハイパーパラメータを使用できます。(辞書で渡すと良いみたいです!)

custom_grid: dictionary, default = None
To use custom hyperparameters for tuning pass a dictionary with parameter name
and values to be iterated. When set to None it uses pre-defined tuning grid.

モデルを比較する

AUC順にソートを行います。

compare_models(sort="AUC")

sortオプションに指定できる値は以下になります。(pycaretのコードから引用)

sort: string, default = 'Accuracy'
The scoring measure specified is used for sorting the average score grid
Other options are 'AUC', 'Recall', 'Precision', 'F1', 'Kappa' and 'MCC'.

実行結果。xgboostで学習を行うと最も精度がよくなるみたいです。

スクリーンショット 2020-09-20 23.02.19

モデルの可視化

matplotlibなどを使うと自分でいろいろ計算してデータを作った上で、グラフを表示しますが、なんと1行でたくさんの情報を可視化してくれます!

evaluate_model(model)

スクリーンショット 2020-09-24 21.22.14

スクリーンショット 2020-09-24 21.22.48

prot_modelで個別のグラフを出してくれます。デフォルトだとAUCのグラフが出力されます。

plot_model(model)

スクリーンショット 2020-09-24 21.26.00

plot_model(model, "pr")

スクリーンショット 2020-09-24 21.26.29

出力できるグラフは以下になります。(pycaretのdocstringより引用)

Plot                    Name
   ------------------      -----------------------           
   'auc'                   Area Under the Curve                 
   'threshold'             Discrimination Threshold           
   'pr'                    Precision Recall Curve                  
   'confusion_matrix'      Confusion Matrix    
   'error'                 Class Prediction Error                
   'class_report'          Classification Report        
   'boundary'              Decision Boundary            
   'rfe'                   Recursive Feature Selection                 
   'learning'              Learning Curve             
   'manifold'              Manifold Learning            
   'calibration'           Calibration Curve         
   'vc'                    Validation Curve                  
   'dimension'             Dimension Learning           
   'feature'               Feature Importance              
   'parameter'             Model Hyperparameter

たった1行でいろんな処理を行ってくれるpycaret、機械学習の知識がなくても実行でき、チューニング・可視化も行ってくれるので本当に便利です!

ハードルが高い機械学習(私もめっちゃハードル高かったです・・・)​ですが、取っ付きやすいと思います!!

この記事が気に入ったらサポートをしてみませんか?