Predict Class Labels Using ClassificationDiscriminant Predict Block
This example shows how to use the ClassificationDiscriminant Predict block for label prediction in Simulink®. The block accepts an observation (predictor data) and returns the predicted class label, class score, and expected classification cost for the observation using the trained discriminant analysis classification model. To complete this example, you can use the provided Simulink model, or create a new model.
Train Classification Model
Load the humanactivity
data set. This data set contains 24,075 observations of five physical human activities: Sitting, Standing, Walking, Running, and Dancing. Each observation has 60 features extracted from acceleration data measured by smartphone accelerometer sensors.
load humanactivity
Create the predictor X
as a numeric matrix that contains 60 features for 24,075 observations. Create the class labels Y
as a numeric vector that contains the activity IDs in integers: 1, 2, 3, 4, and 5 representing Sitting, Standing, Walking, Running, and Dancing, respectively.
X = feat; Y = actid;
Randomly partition observations into a training set and a test set with stratification using the class information in Y
. Use approximately 80% of the observations to train a discriminant analysis model, and 20% of the observations to test the performance of the trained model on new data.
rng(0,"twister") % For reproducibility of the partition cv = cvpartition(Y,"Holdout",0.20);
Extract the training and test indices.
trainingInds = training(cv); testInds = test(cv);
Specify the training and test data sets.
XTrain = X(trainingInds,:); YTrain = Y(trainingInds); XTest = X(testInds,:); YTest = Y(testInds);
Train a discriminant analysis classification model by passing the training data XTrain
and YTrain
to the fitcdiscr
function.
daMdl = fitcdiscr(XTrain,YTrain);
daMdl
is a trained ClassificationDiscriminant
model. You can use dot notation to access the properties of daMdl
. For example, you can enter daMdl.ModelParameters
to get more information about the trained model parameters.
Open Provided Simulink Model
This example provides the Simulink model slexClassificationDAPredictExample.slx
, which includes the ClassificationDiscriminant Predict block. You can open the Simulink model or create a new model as described in the next section.
Open the Simulink model slexClassificationDAPredictExample.slx
.
open_system("slexClassificationDAPredictExample")
When you open the Simulink model, the software runs the code in the PreLoadFcn
callback function before loading the model. The PreLoadFcn
callback function of slexClassificationDAPredictExample
includes code to check if your workspace contains the daMdl
variable for the trained model. If the workspace does not contain the variable, PreLoadFcn
loads the sample data, trains the discriminant analysis classification 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.
Create Simulink Model
To create a new Simulink model, open the Blank Model template and add the ClassificationDiscriminant Predict block from the Classification section of the Statistics and Machine Learning Toolbox™ library.
Double-click the ClassificationDiscriminant Predict block to open the Block Parameters dialog box. Import a trained ClassificationDiscriminant
model into the block by specifying the name of a workspace variable that contains the object. The default variable name is daMdl
, which is the object you created at the command line.
Select the check box for Add output port for predicted class scores to add the second output port score, and select the check box for Add output port for expected classification cost to add the third output port cost. Click OK.
Click the Refresh button to refresh the settings of the trained model in the dialog box. The Trained Machine Learning Model section of the dialog box displays the options used to train the model daMdl
.
Add one Inport block and three Outport blocks, and connect them to the ClassificationDiscriminant Predict block.
The ClassificationDiscriminant Predict block expects an observation containing 60 predictor values, because the model was trained using a data set with 60 predictor variables. Double-click the Inport block, and set Port dimensions to 60 on the Signal Attributes tab. To specify that the output signals have the same length as the input signal, set Sample time to 1 on the Execution tab of the Inport dialog box. Click OK.
At the command line, 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. The orientation must correspond to the observations in the predictor data. In this example,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 predictions.
modelInput.time = (0:length(YTest)-1)'; modelInput.signals(1).values = XTest; modelInput.signals(1).dimensions = size(XTest,2);
Import the signal data from the workspace:
Open the Configuration Parameters dialog box in Simulink. In the Setup section of the Modeling tab, click the top half of the Model Settings button.
In the Data Import/Export pane, select the Input check box and enter
modelInput
in the adjacent text box.In the Solver pane, under Simulation time, set Stop time to
modelInput.time(end)
. Under Solver selection, set Type toFixed-step
, and set Solver todiscrete (no continuous states)
. These settings enable the model to run the simulation for each query point inmodelInput
. Click OK.
For more details, see Load Signal Data for Simulation (Simulink).
Save the model as slexClassificationDAPredictExample.slx
in Simulink.
Simulate Model
Simulate the Simulink model and export the simulation outputs to the workspace. When the Inport block detects an observation, it places the observation into the ClassificationDiscriminant Predict block. You can use the Simulation Data Inspector (Simulink) to view the logged data of an Outport block.
simOut = sim("slexClassificationDAPredictExample");
Determine the simulated classification labels.
outputs = simOut.yout;
sim_label = outputs.get("label").Values.Data;
Create a confusion matrix chart from the true labels (YTest
) and the labels predicted by the Simulink model (sim_label
).
confusionchart(string(YTest),string(sim_label))
Large values on the diagonal indicate accurate predictions for the corresponding class.