Predict Class Labels Using Stateflow
这个例子展示了如何使用一个Stateflow®图for label prediction. The example trains a discriminant analysis model for the Fisher iris data set by usingfitcdiscr
,并定义代码生成的功能,该函数加载了训练有素的模型并预测新数据的标签。该示例中的状态流图接受流数据并使用定义的函数预测标签。
Fisher的虹膜数据集包含在统计和机器学习工具箱中,其中包含物种(species
)和测量(meas
)on sepal length, sepal width, petal length, and petal width for 150 iris specimens. The data set contains 50 specimens from each of three species: setosa, versicolor, and virginica.
加载Fisher Iris数据集。
加载fisheriris
兑换species
to an index vector where 1, 2, and 3 correspond to setosa, versicolor, and virginica, respectively.
物种= grp2idx(物种);
Partition the data into a training set and a test set.
rng('default')% For reproducibilityidx1 = randperm(150,75)'; idx2 = setdiff((1:150)',idx1); X = meas(idx1,:); Y = species(idx1,:); trainX = meas(idx2,:); trainY = species(idx2,:);
UsetrainX
andtrainY
to train a model, and useX
andy
to test the trained model.
Train a quadratic discriminant analysis model.
Mdl = fitcdiscr(trainX,trainY,“歧视”,,,,'quadratic');
Mdl
是一个分类歧视
模型。在命令行,您可以使用Mdl
to make predictions for new observations. However, you cannot useMdl
作为代码生成函数中的输入参数。准备Mdl
通过使用saveLearnerForCoder
。
saveLearnerForCoder(Mdl,``distiris'');
saveLearnerForCoder
compactsMdl
and saves it in the MAT-filedistiris.mat
。
要在状态流模型的显示框中显示预测的物种,请使用ClassDef
MATLAB®文件中的块irisspecies.m
。
ClassDefirisspecies枚举setosa(1)versicolor(2)virginica(3)结尾结尾
For details about enumerated data, seeDefine Enumerated Data Types(状态流)。
定义一个名称的函数mypredict.m
that predicts the iris species from new measurement data by using the trained model. The function should:
Include the code generation directive
%#codegen
somewhere in the function.Accept iris measurement data. The data must be consistent with X except for the number of rows.
Load
distiris.mat
使用LOADLEARNERNERNERFORCODER
。返回预测的虹膜物种。
function标签= mypredict(x)%#codegen%MYPREDICT Predict species of iris flowers using discriminant model% mypredict predicts species of iris flowers using the compact% discriminant model in the file DiscrIris.mat. Rows of X correspond to% observations and columns correspond to predictor variables. label is%预测物种。mdl = loadlearnernerforcoder(``distiris'');labelTemp = predict(mdl,X); label = IrisSpecies(labelTemp);结尾
Open the Simulink® modelsf_countflowers.slx
。
sfname ='sf_countflowers';Open_System(sfname);
这些图显示了Simulink模型和状态流图中包含金宝app的流程图。当输入节点检测到测量数据时,它将数据引导到图表中。然后,图表预测了虹膜花的一种物种,并计算每个物种的花数。该图将预测的物种返回到工作区,并一次显示模型中的物种。数据存储存储器块NumFlowers
stores the number of flowers for each species.
该图希望将输入数据作为一个称为的结构数组接收Fisheririsinput
containing these fields:
时间
- 观测值进入模型的时间点。在示例中,持续时间包括0到74的整数。时间
必须对应于预测数据数据中的观察结果。因此,在此示例中,时间
must be a column vector.信号
- A 1-by-1 structure array describing the input data and containing the fieldsvalues
anddimensions
。Thevalues
field is a matrix of predictor data. Thedimensions
字段是预测变量的数量。
为虹膜花的测量创建适当的结构阵列。
Fisheririsinput。时间= (0:74)'; fisheririsInput.signals.dimensions = 4; fisheririsInput.signals.values = X;
您可以从中更改名称Fisheririsinput
,然后在模型中指定新名称。但是,状态流期望结构数组包含所述的字段名称。有关更多详细信息,请参阅Loading Data Structures to Root-Level Inputs(金宝appSimulink)。
模拟模型。
SIM(sfname)
The figure shows the model after it processes all observations inFisheririsinput
, 一次一个。预测的物种X(75,:)
is virginica. The number of setosa, versicolor, and virginica inX
is 22, 22, and 31, respectively.
The variablelogsout
出现在工作区中。logsout
是一个金宝appsimulinkdata.dataset
包含预测物种的物体。从模拟日志中提取预测的物种数据。
labelsf = logSout.getElement(1).values.data;
使用命令行预测物种predict
。
labelCMD = predict(Mdl,X);
比较返回的预测物种sf_countflowers
通过打电话给那些predict
在命令行。
iSequal(LabelCMD,labelsf)
ans =逻辑1
是平等的
returns logical 1 (true
)如果所有输入都相等。这种比较证实了sf_countflowers
返回预期结果。
如果您还拥有Simulink Code金宝appr™许可证,则可以从sf_countflowers.slx
在Si金宝appmulink或从命令行中使用rtwbuild
(金宝appSimulink编码器)。有关更多详细信息,请参阅Generate C Code for a Model(金宝appSimulink编码器)。
也可以看看
LOADLEARNERNERNERFORCODER
|saveLearnerForCoder
|predict
|Slbuild
(金宝appSimulink)