10.2 アルゴリズムの選択

oml.automl.AlgorithmSelectionクラスは、データセットおよびタスクの特性を使用して、サポートされているOracle Machine Learningアルゴリズムのセットからアルゴリズムをランク付けします。

データセットおよび予測タスクに最適なOracle Machine Learningアルゴリズムを選択するのは簡単なことではありません。すべてのモデリングの問題に最適なアルゴリズムはありません。oml.automl.AlgorithmSelectionクラスは、それぞれが良質のモデルを生成する可能性に従って、候補アルゴリズムをランク付けします。これは、網羅的な検索を回避することを目的として、データセットのレパートリから学習したオラクル社の高度なメタ学習インテリジェンスを使用することで実現されるため、全体的な計算時間とコストが削減されます。

oml.automl.AlgorithmSelectionクラスは、分類および回帰アルゴリズムをサポートしています。このクラスを使用するには、データセットおよび評価するアルゴリズムの数を指定します。

このクラスのselectメソッドは、上位アルゴリズムとその予測ランクを(最良から最悪へという順に)ソートしたリストを返します。

このクラスのパラメータおよびメソッドの詳細は、help(oml.automl.AlgorithmSelection)を呼び出すか、Oracle Machine Learning for Python APIリファレンスを参照してください。

例10-1 oml.automl.AlgorithmSelectionクラスの使用

この例では、oml.automl.AlgorithmSelectionオブジェクトを作成し、アルゴリズムのランク付けを対応するスコア・メトリックとともに表示します。特定のビジネス問題のニーズに応じて、最上位のエントリを選択することも、別のモデルを選択することもできます。

import oml
from oml import automl
import pandas as pd
from sklearn import datasets

# Load the breast cancer data set.
bc = datasets.load_breast_cancer()
bc_data = bc.data.astype(float)
X = pd.DataFrame(bc_data, columns = bc.feature_names)
y = pd.DataFrame(bc.target, columns = ['TARGET'])

# Create the database table BreastCancer.
oml_df = oml.create(pd.concat([X, y], axis=1), 
                               table = 'BreastCancer')

# Split the data set into training and test data.
train, test = oml_df.split(ratio=(0.8, 0.2), seed = 1234)
X, y = train.drop('TARGET'), train['TARGET']
X_test, y_test = test.drop('TARGET'), test['TARGET']

# Create an automated algorithm selection object with f1_macro as
# the score_metric argument.
asel = automl.AlgorithmSelection(mining_function='classification', 
                              score_metric='f1_macro', parallel=4)

# Run algorithm selection to get the top k predicted algorithms and 
# their ranking without tuning.
algo_ranking = asel.select(X, y, k=3)

# Show the selected and tuned model.
[(m, "{:.2f}".format(s)) for m,s in algo_ranking]

# Drop the database table.
oml.drop('BreastCancer')

この例のリスト

>>> import oml
>>> from oml import automl
>>> import pandas as pd
>>> from sklearn import datasets
>>>
>>> # Load the breast cancer data set.
... bc = datasets.load_breast_cancer()
>>> bc_data = bc.data.astype(float)
>>> X = pd.DataFrame(bc_data, columns = bc.feature_names)
>>> y = pd.DataFrame(bc.target, columns = ['TARGET'])
>>>
>>> # Create the database table BreastCancer.
>>> oml_df = oml.create(pd.concat([X, y], axis=1),
...                                table = 'BreastCancer')
>>> 
>>> # Split the data set into training and test data.
... train, test = oml_df.split(ratio=(0.8, 0.2), seed = 1234)
>>> X, y = train.drop('TARGET'), train['TARGET']
>>> X_test, y_test = test.drop('TARGET'), test['TARGET']
>>>
>>> # Create an automated algorithm selection object with f1_macro as 
... # the score_metric argument.
... asel = automl.AlgorithmSelection(mining_function='classification', 
...                               score_metric='f1_macro', parallel=4)
>>>
>>> # Run algorithm selection to get the top k predicted algorithms and  
... # their ranking without tuning.
... algo_ranking = asel.select(X, y, k=3)
>>> 
>>> # Show the selected and tuned model.
>>> [(m, "{:.2f}".format(s)) for m,s in algo_ranking]
[('svm_gaussian', '0.97'), ('glm_ridge', '0.96'), ('nn', '0.96')] 
>>>
>>> # Drop the database table.
... oml.drop('BreastCancer')