主要内容

生成代码to Classify Data in Table

This example shows how to generate code for classifying numeric and categorical data in a table using a binary decision tree model. The trained model in this example identifies categorical predictors in the分类预期property; therefore, the software handles categorical predictors automatically. You do not need to create dummy variables manually for categorical predictors to generate code.

In the general code generation workflow, you can train a classification or regression model on data in a table. You pass arrays (instead of a table) to your entry-point function for prediction, create a table inside the entry-point function, and then pass the table to预测。有关代码生成中表支持的更多信息,请参见金宝appCode Generation for Tables(MATLAB编码器)Table Limitations for Code Generation(MATLAB编码器)

Train Classification Model

加载患者数据集。创建一个包含类型的数字预测指标的表single双倍的, categorical predictors of type分类, and the response variableSmokerof typelogical。表的每一行都对应于另一个患者。

load患者年龄=单个(年龄);重量=单个(重量);性别=分类(性别);selfsessessedHealthStatus =分类(selfsessessedHealthStatus);tbl =表(年龄,舒张期,收缩期,体重,性别,自我评估healthStatus,吸烟者);

Train a classification tree using the data inTBL

mdl = fitctree(tbl,“吸烟者”)
mdl = classificationTree predicterNames:{1x6 cell}响应eNAME:'smoker'分类器:[5 6] classNames:[0 1] scoretransform:'none'numObservations:100个属性,方法,方法,方法

The分类预期property value is[5 6],这表明MDLidentifies the 5th and 6th predictors ('Gender''SelfAssessedHealthStatus') as categorical predictors. To identify any other predictors as categorical predictors, you can specify them by using the'CategoricalPredictors'名称值参数。

MDL

mdl.predictictornames
ans =1x6 cellColumns 1 through 5 {'Age'} {'Diastolic'} {'Systolic'} {'Weight'} {'Gender'} Column 6 {'SelfAssessedHe...'}

保存模型

Save the tree classifier to a file usingSavelearnerforcoder

Savelearnerforcoder(MDL,'treemodel');

Savelearnerforcodersaves the classifier to the MATLAB® binary fileTreeModel.matas a structure array in the current folder.

Define Entry-Point Function

定义入口点功能预测Smoker, which takes predictor variables as input arguments. Within the function, load the tree classifier by usingloadLearnerForCoder,从输入参数创建一个表,然后将分类器和表传递给预测

功能[标签,分数] =预测莫克(年龄,舒张,收缩期,体重,性别,自我评估)%#codegen%PREDICTSMOKER Label new observations using a trained tree model% predictSmoker预测患者是否smokers (1) or nonsmokers% (0) based on their age, diastolic blood pressure, systolic blood% pressure, weight, gender, and self assessed health status. The function%还提供分类分数,表明可能性% 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

当您在入口点函数中创建表时,必须指定变量名(例如,使用'VariableNames'名称值对参数桌子). If your table contains only predictor variables, and the predictors are in the same order as in the table used to train the model, then you can find the predictor variable names inmdl.predictictornames

生成代码

Generate code for预测Smoker通过使用codegen。Specify the data type and dimensions of the predictor variable input arguments usingcoder.typeof

  • 第一个输入参数coder.typeof指定预测变量的数据类型。

  • 第二个输入参数指定行上的上限(inf)和列(1) in the predictor.

  • The third input argument specifies that the number of rows in the predictor can change at run time but the number of columns is fixed.

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预测Smoker-argsARGS
Code generation successful.

codegengenerates the MEX function预测Smoker_mex当前文件夹中带有一个依赖平台的扩展名。

验证生成的代码

Verify that预测,预测Smoker,并且MEX文件返回20例患者的随机样本相同的结果。

rng('默认')%可再现性[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 =logical1
验证mexscores =isequal(scores1,scores2,scores3)
验证mexscores =logical1

See Also

(MATLAB编码器)|(MATLAB编码器)||

相关话题