table のデータを分類するためのコードの生成
この例では、二分決定木モデルを使用して table の数値データおよびカテゴリカル データを分類するためのコードを生成する方法を示します。この例の学習済みモデルでは CategoricalPredictors プロパティでカテゴリカル予測子を識別するため、カテゴリカル予測子が自動的に処理されます。コードを生成するためにカテゴリカル予測子のダミー変数を手動で作成する必要はありません。
一般的なコード生成のワークフローでは、table のデータを分類モデルまたは回帰モデルに学習させることができます。予測のためのエントリポイント関数に (table ではなく) 配列を渡し、エントリポイント関数内に table を作成し、その table を predict に渡します。コード生成における table のサポートの詳細については、table のコード生成 (MATLAB Coder)およびコード生成における table の制限事項 (MATLAB Coder)を参照してください。
分類モデルの学習
patients データ セットを読み込みます。single 型と double 型の数値予測子、categorical 型のカテゴリカル予測子、および logical 型の応答変数 Smoker を格納する table を作成します。table の各行は、異なる患者に対応しています。
load patients
Age = single(Age);
Weight = single(Weight);
Gender = categorical(Gender);
SelfAssessedHealthStatus = categorical(SelfAssessedHealthStatus);
Tbl = table(Age,Diastolic,Systolic,Weight,Gender,SelfAssessedHealthStatus,Smoker);Tbl のデータを使用して分類木に学習させます。
Mdl = fitctree(Tbl,'Smoker')Mdl =
ClassificationTree
PredictorNames: {'Age' 'Diastolic' 'Systolic' 'Weight' 'Gender' 'SelfAssessedHealthStatus'}
ResponseName: 'Smoker'
CategoricalPredictors: [5 6]
ClassNames: [0 1]
ScoreTransform: 'none'
NumObservations: 100
Properties, Methods
CategoricalPredictors プロパティの値が [5 6] であり、Mdl で 5 番目と 6 番目の予測子 ('Gender' と 'SelfAssessedHealthStatus') をカテゴリカル予測子として指定していることを示しています。他の予測子をカテゴリカル予測子として指定する場合は、名前と値の引数 'CategoricalPredictors' を使用して指定できます。
Mdl における予測子の名前と順序を表示します。
Mdl.PredictorNames
ans = 1×6 cell
{'Age'} {'Diastolic'} {'Systolic'} {'Weight'} {'Gender'} {'SelfAssessedHealthStatus'}
モデルの保存
saveLearnerForCoder を使用して、ツリーの分類器をファイルに保存します。
saveLearnerForCoder(Mdl,'TreeModel');saveLearnerForCoder は、現在のフォルダーの TreeModel.mat という MATLAB® バイナリ ファイルに構造体配列として分類器を保存します。
エントリポイント関数の定義
予測子変数を入力引数として受け入れるエントリポイント関数 predictSmoker を定義します。この関数では、loadLearnerForCoder を使用してツリーの分類器を読み込み、入力引数から table を作成した後、分類器および table を predict に渡します。
function [labels,scores] = predictSmoker(age,diastolic,systolic,weight,gender,selfAssessedHealthStatus) %#codegen %PREDICTSMOKER Label new observations using a trained tree model % predictSmoker predicts whether patients are smokers (1) or nonsmokers % (0) based on their age, diastolic blood pressure, systolic blood % pressure, weight, gender, and self assessed health status. The function % also provides classification scores indicating the likelihood that a % predicted label comes from a particular class (smoker or nonsmoker). mdl = loadLearnerForCoder('TreeModel'); varnames = mdl.PredictorNames; tbl = table(age,diastolic,systolic,weight,gender,selfAssessedHealthStatus, ... 'VariableNames',varnames); [labels,scores] = predict(mdl,tbl); end
エントリポイント関数内に table を作成する場合、変数名を指定しなければなりません (table の名前と値のペアの引数 'VariableNames' を使用するなど)。table に予測子変数のみが含まれていて、予測子の順序がモデルの学習に使用した table と同じである場合、予測子変数の名前を mdl.PredictorNames で見つけることができます。
コードの生成
codegen を使用して predictSmoker のコードを生成します。coder.typeof を使用して、予測子変数の入力引数のデータ型と次元を指定します。
coder.typeofの最初の入力引数は、予測子のデータ型を指定します。2 番目の入力引数は、予測子の行数の上限 (
Inf) および列数の上限 (1) を指定します。3 番目の入力引数は、実行時に予測子の行数を変更できるが、列数は固定であることを指定します。
ARGS = cell(4,1);
ARGS{1} = coder.typeof(Age,[Inf 1],[1 0]);
ARGS{2} = coder.typeof(Diastolic,[Inf 1],[1 0]);
ARGS{3} = coder.typeof(Systolic,[Inf 1],[1 0]);
ARGS{4} = coder.typeof(Weight,[Inf 1],[1 0]);
ARGS{5} = coder.typeof(Gender,[Inf 1],[1 0]);
ARGS{6} = coder.typeof(SelfAssessedHealthStatus,[Inf 1],[1 0]);
codegen predictSmoker -args ARGSCode generation successful.
codegen は、プラットフォームに依存する拡張子の MEX 関数 predictSmoker_mex を現在のフォルダーに生成します。
生成されたコードの確認
predict、predictSmoker および MEX ファイルが 20 人の患者の無作為標本に対して同じ結果を返すことを確認します。
rng('default') % For reproducibility [newTbl,idx] = datasample(Tbl,20); [labels1,scores1] = predict(Mdl,newTbl); [labels2,scores2] = predictSmoker(Age(idx),Diastolic(idx),Systolic(idx),Weight(idx),Gender(idx),SelfAssessedHealthStatus(idx)); [labels3,scores3] = predictSmoker_mex(Age(idx),Diastolic(idx),Systolic(idx),Weight(idx),Gender(idx),SelfAssessedHealthStatus(idx)); verifyMEXlabels = isequal(labels1,labels2,labels3)
verifyMEXlabels = logical
1
verifyMEXscores = isequal(scores1,scores2,scores3)
verifyMEXscores = logical
1
参考
codegen (MATLAB Coder) | coder.typeof (MATLAB Coder) | loadLearnerForCoder | saveLearnerForCoder
トピック
- コード生成の紹介
- 機械学習モデルの予測をコマンド ラインで行うコードの生成
- table のコード生成 (MATLAB Coder)
- コード生成における table の制限事項 (MATLAB Coder)