ランダムフォレストコンポーネントは、トレーニングデータのランダムなサブセットに複数の決定木を適合させ、それらの予測を多数決で組み合わせることにより、アンサンブル分類器をトレーニングします。行のブートストラップサンプリング (maxRecordSize) と各分割でのランダムな特徴量選択 (randomColNum) という 2 つのランダム性のソースにより、個々の木の分散が減少し、未知のデータに対する汎化性能が向上します。このコンポーネントは、単一の決定木が過学習する場合や、連続特徴量とカテゴリカル特徴量の両方を扱う堅牢なベースライン分類器が必要な場合に使用します。
コンポーネントの設定
ランダムフォレストコンポーネントは、ビジュアルモデリングのパイプラインキャンバスまたは PAI コマンドを使用して設定します。
方法1:パイプラインキャンバスでの設定
Machine Learning Platform for AI (PAI) のビジュアルモデリングのパイプラインページで、次のパラメーターを設定します。ビジュアルモデリングは、以前は Machine Learning Studio として知られていました。
| タブ | パラメーター | 説明 |
|---|---|---|
| [フィールド設定] | 特徴列 | トレーニングの特徴量として使用される列。デフォルトでは、ラベル列と重み列を除くすべての列が選択されます。 |
| [除外する列] | トレーニングから除外される列。除外された列は特徴列として使用できません。 | |
| [強制変換列] | 型によって強制的に解析する列:STRING、BOOLEAN、DATETIME 型の列は離散値として解析されます。DOUBLE と BIGINT 型の列は連続値として解析されます。BIGINT 型の列をカテゴリカルとして扱うには、forceCategorical パラメーターを使用して指定します。 | |
| [重み列] | 各行のサンプル重みを含む列。数値データ型がサポートされています。 | |
| ラベル列 | 入力テーブルのラベル列。STRING と数値データ型がサポートされています。 | |
| [パラメーター設定] | [フォレスト内の決定木の数] | 木の数。木が多いほど分散は減少しますが、トレーニング時間は増加します。有効値:1~1000。デフォルト:100。 |
| [単一決定木アルゴリズム] | フォレスト内の各位置に割り当てられる木アルゴリズム。algorithmTypes=[a,b] を持つ N 個の木からなるフォレストの場合:位置 [0, a) は ID3 を使用し、位置 [a, b) は CART を使用し、位置 [b, N] は C4.5 を使用します。None に設定した場合、木アルゴリズムはフォレスト全体に均等に分散されます。 | |
| [各決定木のランダム特徴量の数] | 各分割でランダムにサンプリングされる特徴量の数。値が小さいほど木々の間の多様性が増しますが、バイアスが増加する可能性があります。有効値:[1, N]。N は特徴量の総数です。デフォルト:log₂N。 | |
| 最小リーフノード数 | リーフノードに必要な最小サンプル数。この値を増やすと、木が小さなサンプルに過学習するのを防ぎます。有効値:正の整数。デフォルト:2。 | |
| リーフノードと親ノードの最小比率 | 親ノードに対するリーフノードのサンプルの最小比率。有効値:[0, 1]。デフォルト:0。 | |
| [決定木の最大深度] | 単一の木の最大深度。深度を制限すると過学習を減らすことができます。有効値:[1, +∞)。デフォルト:無制限。 | |
| [各決定木のランダムデータ入力数] | 各木のブートストラップサンプルサイズ。有効値:(1000, 1000000]。デフォルト:100000。 |
方法2:PAI コマンドの使用
SQL スクリプトコンポーネントを介して PAI コマンドを実行します。詳細については、「SQL スクリプト」をご参照ください。
PAI -name randomforests
-project algo_public
-DinputTableName="pai_rf_test_input"
-DmodelName="pai_rf_test_model"
-DforceCategorical="f1"
-DlabelColName="class"
-DfeatureColNames="f0,f1"
-DmaxRecordSize="100000"
-DminNumPer="0"
-DminNumObj="2"
-DtreeNum="3";| パラメーター | 必須 | 説明 | デフォルト |
|---|---|---|---|
inputTableName | はい | 入力テーブルの名前。 | — |
inputTablePartitions | いいえ | トレーニングに使用するパーティション。単一レベルのパーティションの場合は partition_name=value、複数レベルのパーティションの場合は name1=value1/name2=value2 のフォーマットです。複数のパーティションはカンマで区切ります。 | すべてのパーティション |
labelColName | はい | ラベル列の名前。 | — |
modelName | はい | 出力モデルの名前。 | — |
treeNum | はい | フォレスト内の木の数。有効値:1~1000。 | 100 |
excludedColNames | いいえ | トレーニングから除外する列。除外された列は特徴列として使用できません。 | 空の文字列 |
weightColName | いいえ | 重み列の名前。 | — |
featureColNames | いいえ | トレーニング用の特徴列。 | labelColName と weightColName |
forceCategorical | いいえ | 指定された BIGINT 型の列をカテゴリカルとして強制的に扱います。デフォルトでは、BIGINT 型の列は連続値として扱われます。STRING、BOOLEAN、DATETIME 型の列は、このパラメーターに関係なく常に離散値として解析されます。 | INT は連続値として扱われます |
algorithmTypes | いいえ | フォレスト内の各位置に割り当てられる木アルゴリズム。algorithmTypes=[a,b] を持つ N 個の木の場合:[0, a) は ID3 を使用し、[a, b) は CART を使用し、[b, N] は C4.5 を使用します。None に設定した場合、アルゴリズムは均等に分散されます。 | 均等に分散 |
randomColNum | いいえ | 各分割でランダムにサンプリングされる特徴量の数。値が小さいほど、より多様な木が生成されます。有効値:[1, N]。N は特徴量の総数です。 | log₂N |
minNumObj | いいえ | リーフノードに必要な最小サンプル数。正の整数である必要があります。 | 2 |
minNumPer | いいえ | 親ノードに対するリーフノードのサンプルの最小比率。有効値:[0, 1]。 | 0.0 |
maxTreeDeep | いいえ | 単一の木の最大深度。有効値:[1, +∞)。 | 無制限 |
maxRecordSize | いいえ | 各木のブートストラップサンプルサイズ。有効値:(1000, 1000000]。 | 100000 |
例
この例では、小規模なデータセットで 3 つの木からなるランダムフォレスト分類器をトレーニングし、サンプルを "good" または "bad" に分類します。
ステップ1:トレーニングテーブルの作成
CREATE TABLE pai_rf_test_input AS
SELECT * FROM
(
SELECT 1 AS f0, 2 AS f1, 'good' AS class
UNION ALL
SELECT 1 AS f0, 3 AS f1, 'good' AS class
UNION ALL
SELECT 1 AS f0, 4 AS f1, 'bad' AS class
UNION ALL
SELECT 0 AS f0, 3 AS f1, 'good' AS class
UNION ALL
SELECT 0 AS f0, 4 AS f1, 'bad' AS class
) tmp;ステップ2:モデルのトレーニング
PAI -name randomforests
-project algo_public
-DinputTableName="pai_rf_test_input"
-Dmodelname="pai_rf_test_model"
-DforceCategorical="f1"
-DlabelColName="class"
-DfeatureColNames="f0,f1"
-DmaxRecordSize="100000"
-DminNumPer="0"
-DminNumObj="2"
-DtreeNum="3";これにより、f0 と f1 を特徴量とし、class をラベルとして、3 つの木からなるフォレストをトレーニングします。列 f1 は forceCategorical を介してカテゴリカル型に強制されます。
ステップ3:モデル出力の確認
トレーニング後、モデルは Predictive Model Markup Language (PMML) ファイルとして保存されます。PMML 出力は、各木を MiningModel 内の TreeModel セグメントとして記述し、多数決を用いて予測を結合します。
<?xml version="1.0" encoding="utf-8"?>
<PMML xmlns="http://www.dmg.org/PMML-4_2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="4.2" xsi:schemaLocation="http://www.dmg.org/PMML-4_2 http://www.dmg.org/v4-2/pmml-4-2.xsd">
<Header copyright="Copyright (c) 2014, Alibaba Inc." description="">
<Application name="ODPS/PMML" version="0.1.0"/>
<Timestamp>Tue, 12 Jul 2016 07:04:48 GMT</Timestamp>
</Header>
<DataDictionary numberOfFields="2">
<DataField name="f0" optype="continuous" dataType="integer"/>
<DataField name="f1" optype="continuous" dataType="integer"/>
<DataField name="class" optype="categorical" dataType="string">
<Value value="bad"/>
<Value value="good"/>
</DataField>
</DataDictionary>
<MiningModel modelName="xlab_m_random_forests_1_75078_v0" functionName="classification" algorithmName="RandomForests"/>
<MiningSchema>
<MiningField name="f0" usageType="active"/>
<MiningField name="f1" usageType="active"/>
<MiningField name="class" usageType="target"/>
</MiningSchema>
<Segmentation multipleModelMethod="majorityVote">
<Segment id="0">
<True/>
<TreeModel modelName="xlab_m_random_forests_1_75078_v0" functionName="classification" algorithmName="RandomForests">
<MiningSchema>
<MiningField name="f0" usageType="active"/>
<MiningField name="f1" usageType="active"/>
<MiningField name="class" usageType="target"/>
</MiningSchema>
<Node id="1">
<True/>
<ScoreDistribution value="bad" recordCount="2"/>
<ScoreDistribution value="good" recordCount="3"/>
<Node id="2" score="good">
<SimplePredicate field="f1" operator="equal" value="2"/>
<ScoreDistribution value="good" recordCount="1"/>
</Node>
<Node id="3" score="good">
<SimplePredicate field="f1" operator="equal" value="3"/>
<ScoreDistribution value="good" recordCount="2"/>
</Node>
<Node id="4" score="bad">
<SimplePredicate field="f1" operator="equal" value="4"/>
<ScoreDistribution value="bad" recordCount="2"/>
</Node>
</Node>
</TreeModel>
</Segment>
<Segment id="1">
<True/>
<TreeModel modelName="xlab_m_random_forests_1_75078_v0" functionName="classification" algorithmName="RandomForests">
<MiningSchema>
<MiningField name="f0" usageType="active"/>
<MiningField name="f1" usageType="active"/>
<MiningField name="class" usageType="target"/>
</MiningSchema>
<Node id="1">
<True/>
<ScoreDistribution value="bad" recordCount="2"/>
<ScoreDistribution value="good" recordCount="3"/>
<Node id="2" score="good">
<SimpleSetPredicate field="f1" booleanOperator="isIn">
<Array n="2" type="integer">2 3</Array>
</SimpleSetPredicate>
<ScoreDistribution value="good" recordCount="3"/>
</Node>
<Node id="3" score="bad">
<SimpleSetPredicate field="f1" booleanOperator="isNotIn">
<Array n="2" type="integer">2 3</Array>
</SimpleSetPredicate>
<ScoreDistribution value="bad" recordCount="2"/>
</Node>
</Node>
</TreeModel>
</Segment>
<Segment id="2">
<True/>
<TreeModel modelName="xlab_m_random_forests_1_75078_v0" functionName="classification" algorithmName="RandomForests">
<MiningSchema>
<MiningField name="f0" usageType="active"/>
<MiningField name="f1" usageType="active"/>
<MiningField name="class" usageType="target"/>
</MiningSchema>
<Node id="1">
<True/>
<ScoreDistribution value="bad" recordCount="2"/>
<ScoreDistribution value="good" recordCount="3"/>
<Node id="2" score="bad">
<SimplePredicate field="f0" operator="lessOrEqual" value="0.5"/>
<ScoreDistribution value="bad" recordCount="1"/>
<ScoreDistribution value="good" recordCount="1"/>
</Node>
<Node id="3" score="good">
<SimplePredicate field="f0" operator="greaterThan" value="0.5"/>
<ScoreDistribution value="bad" recordCount="1"/>
<ScoreDistribution value="good" recordCount="2"/>
</Node>
</Node>
</TreeModel>
</Segment>
</Segmentation>
</MiningModel>
</PMML>PMML 出力で確認すべき主要な要素は次のとおりです。
`DataDictionary`:各特徴列とラベル列を、そのデータ型 (
continuousまたはcategorical) とともにリストします。`Segmentation multipleModelMethod="majorityVote"`:フォレストがすべての木からの予測を多数決で結合することを示します。
`ScoreDistribution`:各ノードでのクラス分布を示します。これは、各木がその分割でサンプルをどれだけ確信を持って分類しているかを示します。
ステップ4:視覚化された出力の表示
