predict class labels using stateflow -凯发k8网页登录
this example shows how to use a stateflow® chart for label prediction. the example trains a discriminant analysis model for the fisher iris data set by using fitcdiscr
, and defines a function for code generation that loads the trained model and predicts labels for new data. the stateflow chart in this example accepts streaming data and predicts labels using the function you define.
fisher's iris data set, which is included in statistics and machine learning toolbox™, contains species (species
) and measurements (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.
load the fisher iris data set.
load fisheriris
convert species
to an index vector where 1, 2, and 3 correspond to setosa, versicolor, and virginica, respectively.
species = grp2idx(species);
partition the data into a training set and a test set.
rng('default') % for reproducibility idx1 = randperm(150,75)'; idx2 = setdiff((1:150)',idx1); x = meas(idx1,:); y = species(idx1,:); trainx = meas(idx2,:); trainy = species(idx2,:);
use trainx
and trainy
to train a model, and use x
and y
to test the trained model.
train a quadratic discriminant analysis model.
mdl = fitcdiscr(trainx,trainy,'discrimtype','quadratic');
mdl
is a model. at the command line, you can use mdl
to make predictions for new observations. however, you cannot use mdl
as an input argument in a function for code generation. prepare mdl
to be loaded within the function by using savelearnerforcoder
.
savelearnerforcoder(mdl,'discriris');
savelearnerforcoder
compacts mdl
and saves it in the mat-file discriris.mat
.
to display the predicted species in the display box of the stateflow model, define an enumeration class by using a classdef
block in the matlab® file irisspecies.m
.
classdef irisspecies < simulink.intenumtype enumeration setosa(1) versicolor(2) virginica(3) end end
for details about enumerated data, see (stateflow).
define a function named 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
discriris.mat
usingloadlearnerforcoder
.return predicted iris species.
function label = 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 % the predicted species. mdl = loadlearnerforcoder('discriris'); labeltemp = predict(mdl,x); label = irisspecies(labeltemp); end
open the simulink® model sf_countflowers.slx
.
sfname = 'sf_countflowers';
open_system(sfname);
the figures display the simulink model and the flow graph contained in the stateflow chart. when the input node detects measurement data, it directs the data into the chart. the chart then predicts a species of iris flower and counts the number of flowers for each species. the chart returns the predicted species to the workspace and displays the species within the model, one at a time. the data store memory block numflowers
stores the number of flowers for each species.
the chart expects to receive input data as a structure array called fisheririsinput
containing these fields:
time
- the points in time at which the observations enter the model. in the example, the duration includes the integers from 0 through 74. the orientation oftime
must correspond to the observations in the predictor data. so, for this example,time
must be a column vector.signals
- a 1-by-1 structure array describing the input data and containing the fieldsvalues
anddimensions
. thevalues
field is a matrix of predictor data. thedimensions
field is the number of predictor variables.
create an appropriate structure array for iris flower measurements.
fisheririsinput.time = (0:74)'; fisheririsinput.signals.dimensions = 4; fisheririsinput.signals.values = x;
you can change the name from fisheririsinput
, and then specify the new name in the model. however, stateflow expects the structure array to contain the described field names. for more details, see (simulink).
simulate the model.
sim(sfname)
the figure shows the model after it processes all observations in fisheririsinput
, one at a time. the predicted species of x(75,:)
is virginica. the number of setosa, versicolor, and virginica in x
is 22, 22, and 31, respectively.
the variable logsout
appears in the workspace. logsout
is a simulinkdata.dataset
object containing the predicted species. extract the predicted species data from the simulation log.
labelsf = logsout.getelement(1).values.data;
predict species at the command line using .
labelcmd = predict(mdl,x);
compare the predicted species returned by sf_countflowers
to those returned by calling predict
at the command line.
isequal(labelcmd,labelsf)
ans = logical
1
isequal
returns logical 1 (true
) if all the inputs are equal. this comparison confirms that sf_countflowers
returns the expected results.
if you also have a simulink coder™ license, then you can generate c code from sf_countflowers.slx
in simulink or from the command line using (simulink coder). for more details, see generate c code for a model (simulink coder).
see also
loadlearnerforcoder
| savelearnerforcoder
| | slbuild
(simulink)