predict class labels using classificationsvm predict block -凯发k8网页登录
this example shows how to use the classificationsvm predict block for label prediction in simulink®. the block accepts an observation (predictor data) and returns the predicted class label and class score for the observation using the trained support vector machine (svm) classification model.
train classification model
this example uses the ionosphere
data set, which contains radar return qualities (y
) and predictor data (x
) of 34 variables. radar returns are either of good quality ('g'
) or bad quality ('b'
).
load the ionosphere
data set. determine the sample size.
load ionosphere
n = numel(y)
n = 351
suppose that the radar returns are detected in sequence, and you have the first 300 observations, but you have not received the last 51 yet. partition the data into present and future samples.
prsntx = x(1:300,:); prsnty = y(1:300); ftrx = x(301:end,:); ftry = y(301:end);
train an svm model using all presently available data. specify predictor data standardization.
svmmdl = fitcsvm(prsntx,prsnty,'standardize',true);
svmmdl
is a classificationsvm
model.
check the negative and positive class names by using the classnames
property of svmmdl
.
svmmdl.classnames
ans = 2x1 cell
{'b'}
{'g'}
the negative class is 'b'
, and the positive class is 'g'
. the output values from the score port of the classificationsvm predict block have the same order. the first and second elements correspond to the negative class and positive class scores, respectively.
create simulink model
this example provides the simulink model slexionosphereclassificationsvmpredictexample.slx
, which includes the classificationsvm predict block. you can open the simulink model or create a new model as described in this section.
open the simulink model slexionosphereclassificationsvmpredictexample.slx
.
simmdlname = 'slexionosphereclassificationsvmpredictexample';
open_system(simmdlname)
if you open the simulink model, then the software runs the code in the preloadfcn
callback function before loading the simulink model. the preloadfcn
callback function of slexionosphereclassificationsvmpredictexample
includes code to check if your workspace contains the svmmdl
variable for the trained model. if the workspace does not contain the variable, preloadfcn
loads the sample data, trains the svm model, and creates an input signal for the simulink model. to view the callback function, in the setup section on the modeling tab, click model settings and select model properties. then, on the callbacks tab, select the preloadfcn
callback function in the model callbacks pane.
to create a new simulink model, open the blank model template and add the classificationsvm predict block. add the inport and outport blocks and connect them to the classificationsvm predict block.
double-click the classificationsvm predict block to open the block parameters dialog box. specify the select trained machine learning model parameter as svmmdl
, which is the name of a workspace variable that contains the trained svm model. click the refresh button. the dialog box displays the options used to train the svm model svmmdl
under trained machine learning model. select the add output port for predicted class scores check box to add the second output port score.
the classificationsvm predict block expects an observation containing 34 predictor values. double-click the inport block, and set the port dimensions to 34 on the signal attributes tab.
create an input signal in the form of a structure array for the simulink model. the structure array must contain these fields:
time
— the points in time at which the observations enter the model. in this example, the duration includes the integers from 0 through 50. the orientation must correspond to the observations in the predictor data. so, in this case,time
must be a column vector.signals
— a 1-by-1 structure array describing the input data and containing the fieldsvalues
anddimensions
, wherevalues
is a matrix of predictor data, anddimensions
is the number of predictor variables.
create an appropriate structure array for future radar returns.
radarreturninput.time = (0:50)'; radarreturninput.signals(1).values = ftrx; radarreturninput.signals(1).dimensions = size(ftrx,2);
to import signal data from the workspace:
open the configuration parameters dialog box. on the modeling tab, click model settings.
in the data import/export pane, select the input check box and enter
radarreturninput
in the adjacent text box.in the solver pane, under simulation time, set stop time to
radarreturninput.time(end)
. under solver selection, set type tofixed-step
, and set solver todiscrete (no continuous states)
.
for more details, see load signal data for simulation (simulink).
simulate the model.
sim(simmdlname);
when the inport block detects an observation, it directs the observation into the classificationsvm predict block. you can use the simulation data inspector (simulink) to view the logged data of the outport blocks.