Main Content

Predict Class Labels Using ClassificationKernel Predict Block

Since R2024b

This example shows how to use the ClassificationKernel 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 Gaussian kernel classification model. To complete this example, you can use the provided Simulink model, or create a new 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 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 Gaussian kernel model using all presently available data. Specify predictor data standardization.

kernelMdl = fitckernel(prsntX,prsntY,Standardize=true);

kernelMdl is a trained ClassificationKernel model. You can use dot notation to access the properties of kernelMdl. For example, you can enter kernelMdl.ModelParameters to get more information about the trained model parameters.

Check the negative and positive class names by using the ClassNames property of kernelMdl.

kernelMdl.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 ClassificationKernel Predict block have the same order. The first and second elements correspond to the negative class and positive class scores, respectively.

Open Provided Simulink Model

This example provides the Simulink model slexClassificationKernelPredictExample.slx, which includes the ClassificationKernel Predict block. You can open the Simulink model or create a new model as described in the next section.

Open the Simulink model slexClassificationKernelPredictExample.slx.

open_system("slexClassificationKernelPredictExample")

When you open the Simulink model, the software runs the code in the PreLoadFcn callback function before loading the Simulink model. The PreLoadFcn callback function of slexClassificationKernelPredictExample includes code to check if your workspace contains the kernelMdl variable for the trained model. If the workspace does not contain the variable, PreLoadFcn loads the sample data, trains the kernel 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 ClassificationKernel Predict block from the Classification section of the Statistics and Machine Learning Toolbox library. Add the Inport and Outport blocks and connect them to the ClassificationKernel Predict block.

Double-click the ClassificationKernel Predict block to open the Block Parameters dialog box. Specify the Select trained machine learning model parameter as kernelMdl, which is the name of a workspace variable that contains the trained kernel model. Click the Refresh button. The dialog box displays the options used to train the kernel model kernelMdl under Trained Machine Learning Model. Select the Add output port for predicted class scores check box to add the second output port score.

Add one Inport block and two Outport blocks, and connect them to the ClassificationKernel Predict block. The 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. 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. 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 fields values and dimensions, where values is a matrix of predictor data, and dimensions 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 to Fixed-step, and set Solver to discrete (no continuous states). These settings enable the model to run the simulation for each query point in radarReturnInput. Click OK.

For more details, see Load Signal Data for Simulation (Simulink).

Save the model as slexClassificationKernelPredictExample.slx in Simulink.

Simulate the 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("slexClassificationKernelPredictExample");

Determine the simulated classification labels.

outputs = simOut.yout;
sim_label = outputs.get("label").Values.Data;

Create a confusion matrix chart from the true labels (ftrY) and the labels predicted by the Simulink model (sim_label).

confusionchart(string(ftrY),string(sim_label))

Figure contains an object of type ConfusionMatrixChart.

Large values on the diagonal indicate accurate predictions for the corresponding class.

See Also

| |

Related Topics