Main Content

Predict Class Labels Using ClassificationNaiveBayes Predict Block

Since R2024a

This example shows how to use the ClassificationNaiveBayes Predict block for label prediction in Simulink®. The block accepts an observation (predictor data) and returns the predicted class label, class score for the observation, and expected classification cost using the trained naive Bayes classification model. To complete this example, you can use the provided Simulink model, or create a new model.

Train Naive Bayes Classifier

Train a naive Bayes classifier and assess the performance of the classifier on a test set.

Load Fisher's iris data set. Create X as a numeric matrix that contains four petal measurements for 150 irises. Create Y as a cell array of character vectors that contains the corresponding iris species.

load fisheriris
X = meas;
Y = species;

For reproducibility of the partition, set the seed for the random number generator. 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 naive Bayes model, and 20% of the observations to test the performance of the trained model on new data.

cv = cvpartition(Y,"HoldOut",0.2);

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 naive Bayes classifier by passing the training data XTrain and YTrain to the fitcnb function. Specify to standardize the predictors with kernel distributions.

nbMdl = fitcnb(Xtrain,Ytrain,Standardize=true, ...
nbMdl = 
              ResponseName: 'Y'
     CategoricalPredictors: []
                ClassNames: {'setosa'  'versicolor'  'virginica'}
            ScoreTransform: 'none'
           NumObservations: 120
         DistributionNames: {'kernel'  'kernel'  'kernel'  'kernel'}
    DistributionParameters: {3x4 cell}
                    Kernel: {'normal'  'normal'  'normal'  'normal'}
                   Support: {'unbounded'  'unbounded'  'unbounded'  'unbounded'}
                     Width: [3x4 double]
                        Mu: [5.8008 3.0433 3.7283 1.1992]
                     Sigma: [0.8360 0.4284 1.7541 0.7684]

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

Evaluate the performance of the classifier on the test set by computing the test set classification accuracy.

testError = loss(nbMdl,Xtest,Ytest,LossFun="classiferror");
testAccuracy = 1 - testError
testAccuracy = 0.9333

Open Provided Simulink Model

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

Open the Simulink model slexClassificationNBPredictExample.slx.


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 slexClassificationNBPredictExample includes code to check if your workspace contains the nbMdl variable for the trained model. If the workspace does not contain the variable, PreLoadFcn loads the sample data, trains the naive Bayes model, and creates an input signal (query points) 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 ClassificationNaiveBayes Predict block from the Classification section of the Statistics and Machine Learning Toolbox™ library.

Double-click the ClassificationNaiveBayes Predict block to open the Block Parameters dialog box. Import a trained ClassificationNaiveBayes model into the block by specifying the name of a workspace variable that contains the object. The default variable name is nbMdl, 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 nbMdl. The Distribution method section displays information about the trained naive Bayes model.

Add one Inport block and three Outport blocks, and connect them to the ClassificationNaiveBayes Predict block.

The ClassificationNaiveBayes Predict block expects an observation containing four predictor values, because the model was trained using a data set with four predictor variables. Double-click the Inport block, and set Port dimensions to 4 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 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 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 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 modelInput. Click OK.

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

Save the model as slexClassificationNBPredictExample.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 ClassificationNaiveBayes Predict block. You can use the Simulation Data Inspector (Simulink) to view the logged data of an Outport block.

simOut = sim("slexClassificationNBPredictExample");

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).


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

See Also

| | | | | | |

Related Topics