Predict Class Labels Using ClassificationSVM Predict Block
This example shows how to use theClassificationSVM Predictblock 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 theionosphere
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 theionosphere
data set. Determine the sample size.
loadionospheren = 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 aClassificationSVM
model.
Check the negative and positive class names by using theClassNames
property ofsvmMdl
.
svmMdl.ClassNames
ans =2x1 cell{'b'} {'g'}
The negative class is'b'
, and the positive class is'g'
. The output values from thescoreport 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 modelslexIonosphereClassificationSVMPredictExample.slx
, which includes theClassificationSVM Predictblock. You can open the Simulink model or create a new model as described in this section.
Open the Simulink modelslexIonosphereClassificationSVMPredictExample.slx
.
SimMdlName ='slexIonosphereClassificationSVMPredictExample'; open_system(SimMdlName)
ThePreLoadFcn
callback function ofslexIonosphereClassificationSVMPredictExample
includes code to load the sample data, train the SVM model, and create an input signal for the Simulink model. If you open the Simulink model, then the software runs the code inPreLoadFcn
before loading the Simulink model. To view the callback function, in theSetupsection on theModelingtab, clickModel Settingsand selectModel Properties. Then, on theCallbackstab, select thePreLoadFcn
callback function in theModel callbackspane.
To create a new Simulink model, open theBlank Modeltemplate 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选择训练的机器学习模型parameter assvmMdl
, which is the name of a workspace variable that contains the trained SVM model. Click theRefreshbutton. The dialog box displays the options used to train the SVM modelsvmMdl
underTrained Machine Learning Model. Select theAdd output port for predicted class scorescheck box to add the second output portscore.
The ClassificationSVM Predict block expects an observation containing 34 predictor values. Double-click the Inport block, and set thePort dimensionsto 34 on theSignal Attributestab.
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 theModelingtab, clickModel Settings.
In theData Import/Exportpane, select theInputcheck box and enter
radarReturnInput
in the adjacent text box.In theSolverpane, underSimulation time, setStop timeto
radarReturnInput.time(end)
. UnderSolver selection, setTypetoFixed-step
, and setSolvertodiscrete (no continuous states)
.
For more details, seeLoad 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 theSimulation Data Inspector(Simulink)to view the logged data of the Outport blocks.