【機械学習】AutoGluonの使い方・クイックスタートの解説(表形式データ)
「AutoGluon」の使い方を、公式のクイックスタートを解説する形で紹介します。
AutoGluonは、AutoML(Auto Machine Learning)を実現するライブラリです。特徴量の設計など機械学習で大変な部分を自動化してくれます。
AutoGluonは数あるAutoMLライブラリの一種で、テキスト、画像、表データに対応しています。
本記事では、AutoGluonのインストール方法、表形式データのクイックスタートを紹介します。
[1] インストール方法
以下は、LinuxかつGPU無しのインストール手順です。GPU有りや別OSの手順も公式に載っています。
python3 -m pip install -U pip --user python3 -m pip install -U setuptools wheel --user python3 -m pip install -U "mxnet<2.0.0" --user python3 -m pip install autogluon --user
AutoGluon: AutoML for Text, Image, and Tabular Data — AutoGluon Documentation 0.2.0 documentation
[2] クイックスタート
公式では下記4種類のクイックスタートが載っています。
- 表形式データ
- 画像分類
- 物体検出
- テキスト分類
本記事では、表形式データの内容を紹介します。
[2-1] 概要
ある人の収入が5万ドルを超えるかどうかを予測する分類モデルを作成するという内容です。
Predicting Columns in a Table - Quick Start — AutoGluon Documentation 0.2.0 documentation
[2-2] 学習データの確認
from autogluon.tabular import TabularDataset, TabularPredictor train_data = TabularDataset('https://autogluon.s3.amazonaws.com/datasets/Inc/train.csv') subsample_size = 500 # subsample subset of data for faster demo, try setting this to much larger values train_data = train_data.sample(n=subsample_size, random_state=0) train_data.head()
500人中、年収5万ドル以下の人が365人、残りの135人が5万ドル以上になります。
label = 'class' print("Summary of class variable: \n", train_data[label].describe()) # Summary of class variable: # count 500 # unique 2 # top <=50K # freq 365 # Name: class, dtype: object
[2-3] 学習の実行
TabularPredictor.fit()
で学習を実行します。
save_path = 'agModels-predictClass' # specifies folder to store trained models predictor = TabularPredictor(label=label, path=save_path).fit(train_data) # Summary of class variable: # count 500 # unique 2 # top <=50K # freq 365 # Name: class, dtype: object
[2-4] 予測と評価
predict
でテストデータを用いた予測を行い、evaluate_predictions
で評価を行います。
test_data = TabularDataset('https://autogluon.s3.amazonaws.com/datasets/Inc/test.csv') y_test = test_data[label] # values to predict test_data_nolab = test_data.drop(columns=[label]) # delete label column to prove we're not cheating test_data_nolab.head()
predictor = TabularPredictor.load(save_path) # unnecessary, just demonstrates how to load previously-trained predictor from file y_pred = predictor.predict(test_data_nolab) print("Predictions: \n", y_pred) # Predictions: # 0 <=50K # 1 <=50K # 2 >50K # 3 <=50K # 4 <=50K # ... # 9764 <=50K # 9765 <=50K # 9766 <=50K # 9767 <=50K # 9768 <=50K # Name: class, Length: 9769, dtype: object
perf = predictor.evaluate_predictions(y_true=y_test, y_pred=y_pred, auxiliary_metrics=True) # Evaluation: accuracy on test data: 0.8397993653393387 # Evaluations on test data: # { # "accuracy": 0.8397993653393387, # "balanced_accuracy": 0.7437076677780596, # "mcc": 0.5295565206264157, # "f1": 0.6242496998799519, # "precision": 0.7038440714672441, # "recall": 0.5608283002588438 # }
また、leaderboard
で訓練を行った各モデルの性能を確認することができます。
predictor.leaderboard(test_data, silent=True)
[2-5] 学習(fit)の中で行われていること
クイックスタートの説明を要約すると次の通りです。
- 二値分類問題であることは自動で認識する。
- どの特徴量が連続値か離散値を自動的に判別する。
- 欠損データや特徴量の再スケーリングなども自動的に処理する。
- 検証用データを使って、複数のモデルを訓練する。
- ハイパーパラメータの選定も自動で行われる。
- 検証用データを指定しない場合は、学習用データからランダムに検証用データを抽出する。
[2-6] 予測精度の最大化
fit()
のパラメータを変えることで予測精度の向上を図ることが可能です。
time_limit = 60 # for quick demonstration only, you should set this to longest time you are willing to wait (in seconds) metric = 'roc_auc' # specify your evaluation metric here predictor = TabularPredictor(label, eval_metric=metric).fit(train_data, time_limit=time_limit, presets='best_quality') predictor.leaderboard(test_data, silent=True)
上記例では、次のことを行っています。
time_limit
で学習時間を60秒まで可能にする。metric
は評価指標が事前に分かっている場合に指定する。指定できるのはroc_auc
,log_loss
,mean_absolute_error
など。presets
で精度や速度などの優先順位を指定する。best_quality
は精度優先、デフォルトは'medium_quality_faster_train
。
[2-7] 回帰の例
ラベルを年齢('age')にすることで、回帰を行うことも可能です。
age_column = 'age' print("Summary of age variable: \n", train_data[age_column].describe()) # Summary of age variable: # count 500.00000 # mean 39.65200 # std 13.52393 # min 17.00000 # 25% 29.00000 # 50% 38.00000 # 75% 49.00000 # max 85.00000 # Name: age, dtype: float64 predictor_age = TabularPredictor(label=age_column, path="agModels-predictAge").fit(train_data, time_limit=60) performance = predictor_age.evaluate(test_data) predictor_age.leaderboard(test_data, silent=True)
終わりに
あまり細かいことを意識することなく使用することが出来ました。面倒な前処理を自動でやってくれるのは大きいメリットです。
機械学習の精度を向上させるためには、特徴量の作成や選択、モデルのチューニングなど様々なテクニックが必要です。AutoGluonのみでは通用しない場面も出てくるでしょうが、手間が圧倒的に少ないため、AutoGluonを初手で使ってみる価値はあるでしょう。
参考文献
- AutoMLとは?自動化された機械学習の可能性と代表的なツール
- https://pages.awscloud.com/rs/112-TZM-766/images/1.AWS_AutoML_AutoGluon.pdf
出典
- アイキャッチはGerd AltmannによるPixabayからの画像
補足
[補足1]
Jupyter Notebookで以下の警告が出ることがあります。
DeprecationWarning:
should_run_async
will not calltransform_cell
automatically in the future. Please pass the result totransformed_cell
argument and any exception that happen during thetransform inpreprocessing_exc_tuple
in IPython 7.17 and above.
非推奨の警告なので動作には影響ないようです。表示しないようにするには、下記URLにある解決策が現状有力なようでした。私の環境でも下記内容で解決しました。
$ python3 -m pip install --upgrade ipykernel $ python3 -m pip install install ipython==7.10.0 --user
ipythonをダウングレードすれば解決します。それがイヤな場合はソースで警告の無視を設定して解決できます。
import warnings warnings.filterwarnings("ignore", category=DeprecationWarning)