PyCaretとは
公式ページ https://pycaret.org/
- 短いコードで機械学習ができる
- 自動化 (AutoML)
- オープンソース
- scikit-learnや他の機械学習パッケージのwrapper
Agenda
- 回帰
- 分類
- クラスタリング
他にも,異常検出(anomaly detection),自然言語処理(natural language processing: NLP),アソシエーション・ルール・マイニングが含まれている.
# from pycaret.utils import enable_colab
# enable_colab()
広告による売り上げの予測
広告のデータ http://logopt.com/data/Advertising.csv を用いる.
テレビ(TV),ラジオ(Radio),新聞(Newspaper)への広告から売り上げ(Sales)を予測する.
import pandas as pd # まずはpandasモジュールを準備する.
df = pd.read_csv(
"http://logopt.com/data/Advertising.csv", index_col=0
) # 0行目をインデックスにする.
df.tail()
独立変数(特徴ベクトル)$X$ は TV, Radio, Newspaperの列,従属変数(ターゲット) $y$ は Salesの列
PyCaretの基本手順
手順1: setup(データフレーム)で準備をする. 引数 target でターゲットの列を指定. 引数 session_id で乱数の種を指定.
手順2: compare_modelsでモデルの比較を行う. 引数 fold で交差検証用のデータの分割数を指定. 返値は最良の評価値のモデルインスタンス. (もしくはcreate_modelでモデルを生成する.)
注意: 遅い計算機で実行する際には,計算時間がかかるモデルを除いておくと良い.引数excludeで除きたいモデルのリストを入れる. たとえば, compare_models(exclude = ['catboost']) とするとCatBoostを除いてモデルの比較を行ってくれる.
- 手順3: predict_modelで予測を行う.
from pycaret.regression import * # 回帰関連の関数のインポート
reg = setup(df, target="Sales", session_id=123)
best_model = compare_models(fold=5)
回帰モデル
No. | 略称 | 回帰モデル | 概要 |
---|---|---|---|
1 | et | Extra Trees Regressor | ランダムに分割してアンサンブルする決定木ベースの手法 |
2 | gbr | Gradient Boosting Regressor | 勾配ブースティング法 |
3 | xgboost | Extreme Gradient Boosting | xgブースト (勾配ブースティング法に正則化を追加) |
4 | rf | Random Forest Regressor | ランダム森(ブートストラップによるランダムサンプリングと決定木のアンサンブル) |
5 | catboost | CatBoost Regressor | カテゴリー変数の扱いに工夫を入れた勾配ブースティング法 |
6 | ada | AdaBoost Regressor | 適応型の勾配ブースティング法 |
7 | dt | Decision Tree Regressor | 決定木 |
8 | lightgbm | Light Gradient Boosting Machine | 勾配ブースティング法の軽量版 |
9 | knn | K Neighbors Regressor | $k$-近傍法 |
10 | lasso | Lasso Regression | Lasso回帰(正則化を入れた線形回帰) |
11 | en | Elastic Net | Elastic Net(正則化を入れた線形回帰) |
12 | lar | Least Angle Regression | 予測値と教師データの偏差と相関が大きい特徴量を1つずつ追加していく方法 |
13 | lr | Linear Regression | 線形回帰 |
14 | ridge | Ridge Regression | リッジ回帰(正則化を入れた線形回帰) |
15 | br | Bayesian Ridge | ベイズリッジ回帰 |
16 | huber | Huber Regressor | Huber回帰 |
17 | omp | Orthogonal Matching Pursuit | 貪欲に特徴量を1つずつ追加していく方法 |
18 | llar | Lasso Least Angle Regression | LassoにLeast Angle Regressionを適用して特徴量選択 |
19 | par | Passive Aggressive Regressor | オンライン型の学習 |
回帰モデルの評価尺度
- MAE 平均絶対誤差 (mean absolute error)
誤差の絶対値の平均値であり,$i$番目のデータの正解(目標値)を$y_i$,予測値を$\hat{y}_{i}$としたとき,以下のように定義される.
$$MAE = \frac{\sum_{i=1}^n |\hat y_i - y_i|}{n}$$
- MSE 平均2乗誤差 (mean squared error)
誤差の2乗の平均値であり,$i$番目のデータの正解(目標値)を$y_i$,予測値を$\hat{y}_{i}$としたとき,以下のように定義される.
$$MSE = \frac{\sum_{i=1}^n (\hat y_i - y_i)^2}{n}$$
- RMSE 平均2乗誤差の平方根 (root mean squared error)
MSEの平方根をとったものがRMSEである.
$$RMSE =\sqrt{\frac{\sum_{i=1}^n (\hat y_i - y_i)^2}{n}}$$
- R2 決定係数(coefficient of determination) $R^2$
回帰モデルによって実データをどれくらい説明できているかを表す指標であり,1に近いほど精度が良いと解釈できる.
$$R^2 = 1 - {\sum_{i=1}^n (y_i - \hat{y}_i)^2 \over \sum_{i=1}^n (y_i-\bar{y})^2 }$$
この定義だと(記号が$R^2$であるにもかかわらず)負になる場合もあるので,注意を要する.最大値は1で誤差が0の状態である.$R^2$が0とは,平均で予測をした場合と同じ精度という意味であり,負の場合は平均値より悪い予測を意味する.
- RMSLE 平均2乗対数誤差の平方根 (root mean squared logarithmic error)
予測値,正解ともに対数をとったもので評価した平均2乗誤差の平方根.
- MAPE 平均絶対パーセント誤差 (mean absolute percentage error)
$$MAPE = \frac{\sum_{i=1}^n | (\hat y_i - y_i)/y_i) |}{n}$$
best_model
best_model_results = pull() # 結果をデータフレームとして得る.
best_model_results.to_csv("best_model.csv")
predict_model(best_model)
可視化(回帰)
可視化の基本手順
- 手順1: plot_model(モデルインスタンス)で描画する.引数plotで描画の種類を指定する.既定値は残差プロット.
- 手順2:interpret_model(モデルインスタンス)で,結果の解釈を可視化する.
plot_modelの引数plotの種類
- ‘residuals’: 残差プロット(既定値)
- ‘error’ : 誤差プロット
- ‘cooks’: Cookの距離プロット(外れ値をみる)
- ‘feature': 特徴重要度プロット
- ‘learning’: 学習曲線
- ‘vc’: 検証曲線
- ‘manifold’: 次元削減を行い特徴を2次元に射影した図
- ‘parameter’: モデルのパラメータを表で表示
- ‘tree’: 決定木の図示(木ベースの場合のみ)
plot_model(best_model)
plot_model(best_model, plot="error")
plot_model(best_model, plot="cooks")
plot_model(best_model, plot="feature")
plot_model(best_model, plot="learning")
plot_model(best_model, plot="vc")
plot_model(best_model, plot="manifold")
plot_model(best_model, plot="parameter")
小さな(深さ3の)決定木 (dt) のモデルを作って可視化する.
dt = create_model("dt", max_depth=3)
plot_model(dt, plot="tree")
interpret_model(best_model)
interpret_model(best_model, plot="correlation")
interpret_model(best_model, plot="reason", observation=10)
問題 (SAT,GPA)
http://logopt.com/data/SATGPA.csv データを用いて,2種類のSATの成績からGPAを予測せよ.
データをそのまま使うと'MathSAT'列と'VerbalSAT'列をカテゴリー変数としてしまうので,浮動小数点数に変換しておく.
gpa = pd.read_csv(
"http://logopt.com/data/SATGPA.csv",
index_col=0,
dtype={"MathSAT": float, "VerbalSAT": float},
)
gpa.head()