Main Content

IncrementalClassificationNaiveBayes Predict

Classify observations using incremental naive Bayes classification model

Since R2025a

  • IncrementalClassificationNaiveBayes Predict Block Icon

Libraries:
Statistics and Machine Learning Toolbox / Incremental Learning / Classification / NaiveBayes

Description

The IncrementalClassificationNaiveBayes Predict block classifies observations using a trained naive Bayes classification model returned as the output of an IncrementalClassificationNaiveBayes Fit block.

Import an initial naive Bayes classification model object into the block by specifying the name of a workspace variable that contains the object. The input port mdl receives a bus signal that represents an incremental learning model fit to streaming data. The input port x receives a chunk of predictor data (observations), and the output port label returns predicted class labels for the chunk. The optional output port score returns predicted class scores or posterior probabilities. The optional output port cost returns the expected classification costs. The optional output port CanPredict returns the prediction status of the trained model.

Examples

expand all

This example shows how to use the IncrementalClassificationNaiveBayes Fit and IncrementalClassificationNaiveBayes Predict blocks for incremental learning and multiclass classification in Simulink®.

Load the human activity data set and randomly shuffle the data. For details on the data set, enter Description at the command line. Responses can be one of five classes: Sitting, Standing, Walking, Running, or Dancing.

load humanactivity
n = numel(actid);
rng(0,"twister") % For reproducibility
idx = randsample(n,n);
X = feat(idx,:);
Y = actid(idx);

Create an initial incremental classification naive Bayes model. The model is configured with 60 predictors, class names, and a metrics warmup period of 200 observations.

nbMdl = incrementalClassificationNaiveBayes(NumPredictors=60,ClassNames=[1,2,3,4,5], MetricsWarmupPeriod=200);

To demonstrate streaming, divide the training data into chunks of 50 observations. For each chunk, select a single observation as a test set to import into the IncrementalClassificationNaiveBayes Predict block.

numObsPerChunk = 50;
nchunk = floor(n/numObsPerChunk);
numPredictors = size(feat,2);
Xin = zeros(numObsPerChunk,numPredictors,nchunk);
Yin = zeros(numObsPerChunk,nchunk);
Xtest = zeros(1,numPredictors,nchunk);
for j = 1:nchunk
    ibegin = min(n,numObsPerChunk*(j-1) + 1);
    iend = min(n,numObsPerChunk*j);
    idx = ibegin:iend;   
    Xin(:,:,j) = X(idx,:);
    Yin(:,j) = Y(idx);
    Xtest(1,:,j) = X(idx(1),:);
 end

Convert the training and test set chunks into time series objects.

t = 0:size(Xin,3)-1;
Xtrain_ts = timeseries(Xin,t,InterpretSingleRowDataAs3D=true);
Ytrain_ts = timeseries(Yin',t,InterpretSingleRowDataAs3D=true);
Xtest_ts = timeseries(Xtest,t,InterpretSingleRowDataAs3D=true);

This example provides a Simulink model, slexIncClassNBPredictExample.slx, shown in the figure below. The model is configured to use incrementalClassificationNaiveBayes as the initial model for the fit block.

slName = "slexIncClassNBPredictExample";
open_system(slName);

Simulate the model and export the simulation outputs to the workspace. You can use the Simulation Data Inspector (Simulink) to view the logged data of an Outport block.

simOut = sim(slName,"StopTime",num2str(numel(t)-1));
% Extract labels
label_sig = simOut.yout.getElement(1);
label_sl = squeeze(label_sig.Values.Data);

% Extract scores values
scores_sig = simOut.yout.getElement(2);
scores_sl = squeeze(scores_sig.Values.Data);

% Extract cost values
cost_sig = simOut.yout.getElement(3);
cost_sl = squeeze(cost_sig.Values.Data);

At each iteration, the IncrementalClassificationNaiveBayes Fit block fits a chunk of observations (predictor data) and outputs the updated incremental learning model parameters as a bus signal. The IncrementalClassificationNaiveBayes Predict block calculates the predicted label for each test set observation.

To see how the model parameters and response values evolve during training, plot them on separate tiles.

figure
tiledlayout(3,1);
nexttile
plot(scores_sl(1,:),".")
ylabel("Score")
xlabel("Iteration")
xlim([0 nchunk])
nexttile
plot(label_sl,".")
ylabel("Label")
xlabel("Iteration")
xlim([0 nchunk])
nexttile
plot(cost_sl(1,:),".")
ylabel("Cost")
xlabel("Iteration")
xlim([0 nchunk])

Figure contains 3 axes objects. Axes object 1 with xlabel Iteration, ylabel Score contains a line object which displays its values using only markers. Axes object 2 with xlabel Iteration, ylabel Label contains a line object which displays its values using only markers. Axes object 3 with xlabel Iteration, ylabel Cost contains a line object which displays its values using only markers.

Ports

Input

expand all

Incremental learning model (incrementalClassificationNaiveBayes) fit to streaming data, specified as a bus signal (see Composite Signals (Simulink)).

Chunk of predictor data, specified as a numeric matrix.

The block supports only numeric input predictor data. If your input data includes categorical data, you must prepare an encoded version of the categorical data. Use dummyvar to convert each categorical variable to a numeric matrix of dummy variables. Then, concatenate all dummy variable matrices and any other numeric predictors. For more details, see Dummy Variables.

Data Types: single | double | half | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean | fixed point

Output

expand all

Chunk of predicted class labels, returned as a column vector. The predicted class is the class that minimizes the expected classification cost. For more details, see the More About section of the predict object function.

Data Types: single | double | half | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean | fixed point | enumerated

Predicted class scores or posterior probabilities, returned as a n-by-numel(mdl.ClassNames) matrix, where n is the number of observations in x. The classification score score(j,k) represents the posterior probability that the observation j in x belongs to class k.

To check the order of the classes, use the ClassNames property of the naive Bayes model specified by Select initial trained machine learning model.

Dependencies

To enable this port, select the check box for Add output port for predicted class scores on the Main tab of the Block Parameters dialog box.

Data Types: single | double | half | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean | fixed point

Expected misclassification costs, returned as a n-by-numel(mdl.ClassNames) matrix, where n is the number of observations in x. The classification cost Cost(j,k) represents the cost of the observation in row j of x being classified into class k.

To check the order of the classes, use the ClassNames property of the naive Bayes model specified by Select initial trained machine learning model.

Dependencies

To enable this port, select the check box for Add output port for expected classification cost on the Main tab of the Block Parameters dialog box.

Data Types: single | double | half | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean | fixed point

Model status for prediction, returned as numeric or logical 0 (false) or 1 (true).

Note

If you specify a model that is not trained, then the IncrementalClassificationNaiveBayes Predict cannot predict the response and the model status is 0 (false).

Dependencies

To enable this port, select the check box for Add output port for status of trained machine learning model on the Main tab of the Block Parameters dialog box.

Parameters

expand all

To edit block parameters interactively, use the Property Inspector. From the Simulink® Toolstrip, on the Simulation tab, in the Prepare gallery, select Property Inspector.

Main

Specify the name of a workspace variable that contains the configured incrementalClassificationNaiveBayes model object.

The following restrictions apply:

  • The predictor data cannot include categorical predictors (logical, categorical, char, string, or cell). If you supply training data in a table, the predictors must be numeric (double or single). To include categorical predictors in a model, preprocess them by using dummyvar before fitting the model.

  • The ScoreTransform property of the initial model cannot be "invlogit" or an anonymous function.

  • The value of the DistributionNames name-value argument cannot be "mn" or "mvmn".

Programmatic Use

Block Parameter: InitialLearner
Type: character vector or string
Values: incrementalClassificationNaiveBayes object name
Default: "nbMdl"

Select the check box to include the output port score for predicted class scores in the IncrementalClassificationNaiveBayes Predict block.

Programmatic Use

Block Parameter: ShowOutputScore
Type: character vector or string
Values: "off" | "on"
Default: "off"

Select the check box to include the output port cost in the IncrementalClassificationNaiveBayes Predict block.

Programmatic Use

Block Parameter: ShowOutputCost
Type: character vector or string
Values: "off" | "on"
Default: "off"

Select the check box to include the output port CanPredict in the IncrementalClassificationNaiveBayes Predict block. This check box does not appear if the workspace already contains an incremental Naive Bayes classification model named nbMdl capable of prediction when you created the IncrementalClassificationNaiveBayes Predict block. Alternatively, you can specify to include the output port CanPredict by selecting theIncrementalClassificationNaiveBayes Predict block in the Simulink workspace and entering set_param(gcb,ShowOutputCanPredict="on") at the MATLAB command line.

Programmatic Use

Block Parameter: ShowOutputCanPredict
Type: character vector or string
Values: "off" | "on"
Default: "off"

Specify the discrete interval between sample time hits or specify another type of sample time, such as continuous (0) or inherited (–1). For more options, see Types of Sample Time (Simulink).

By default, the IncrementalClassificationNaiveBayes Predict block inherits sample time based on the context of the block within the model.

Programmatic Use

Block Parameter: SystemSampleTime
Type: string scalar or character vector
Values: scalar
Default: "–1"

Data Types

Fixed-Point Operational Parameters

Specify the rounding mode for fixed-point operations. For more information, see Rounding Modes (Fixed-Point Designer).

Block parameters always round to the nearest representable value. To control the rounding of a block parameter, enter an expression into the mask field using a MATLAB® rounding function.

Programmatic Use

Block Parameter: RndMeth
Type: character vector
Values: "Ceiling" | "Convergent" | "Floor" | "Nearest" | "Round" | "Simplest" | "Zero"
Default: "Floor"

Specify whether overflows saturate or wrap.

ActionRationaleImpact on OverflowsExample

Select this check box (on).

Your model has possible overflow, and you want explicit saturation protection in the generated code.

Overflows saturate to either the minimum or maximum value that the data type can represent.

The maximum value that the int8 (signed 8-bit integer) data type can represent is 127. Any block operation result greater than this maximum value causes overflow of the 8-bit integer. With the check box selected, the block output saturates at 127. Similarly, the block output saturates at a minimum output value of –128.

Clear this check box (off).

You want to optimize the efficiency of your generated code.

You want to avoid overspecifying how a block handles out-of-range signals. For more information, see Troubleshoot Signal Range Errors (Simulink).

Overflows wrap to the appropriate value that the data type can represent.

The maximum value that the int8 (signed 8-bit integer) data type can represent is 127. Any block operation result greater than this maximum value causes overflow of the 8-bit integer. With the check box cleared, the software interprets the value causing the overflow as int8, which can produce an unintended result. For example, a block result of 130 (binary 1000 0010) expressed as int8 is –126.

Programmatic Use

Block Parameter: SaturateOnIntegerOverflow
Type: character vector
Values: "off" | "on"
Default: "off"

Select this parameter to prevent the fixed-point tools from overriding the data type you specify for the block. For more information, see Use Lock Output Data Type Setting (Fixed-Point Designer).

Programmatic Use

Block Parameter: LockScale
Type: character vector
Values: "off" | "on"
Default: "off"

Data Type

Specify the data type for the label output. The type can be inherited, specified as an enumerated data type, or expressed as a data type object such as Simulink.NumericType.

The supported data types depend on the labels used in the model specified by Select initial trained machine learning model.

  • If the model uses numeric or logical labels, the supported data types are Inherit: Inherit via back propagation (default), double, single, half, int8, uint8, int16, uint16, int32, uint32, int64, uint64, boolean, fixed point, and a data type object.

  • If the model uses nonnumeric labels, the supported data types are Inherit: auto (default), Enum: <class name>, and a data type object.

When you select an inherited option, the software behaves as follows:

  • Inherit: Inherit via back propagation (default for numeric and logical labels) — Simulink automatically determines the Label data type of the block during data type propagation (see Data Type Propagation (Simulink)). In this case, the block uses the data type of a downstream block or signal object.

  • Inherit: auto (default for nonnumeric labels) — The block uses an autodefined enumerated data type variable. For example, suppose the workspace variable name specified by Select initial trained machine learning model is myMdl, and the class labels are class 1 and class 2. Then, the corresponding label values are myMdl_enumLabels.class_1 and myMdl_enumLabels.class_2. The block converts the class labels to valid MATLAB identifiers by using the matlab.lang.makeValidName function.

For more information about data types, see Control Data Types of Signals (Simulink).

Click the Show data type assistant button to display the Data Type Assistant, which helps you set the data type attributes. For more information, see Specify Data Types Using Data Type Assistant (Simulink).

Programmatic Use

Block Parameter: LabelDataTypeStr
Type: character vector or string
Values: "Inherit: Inherit via back propagation" | "Inherit: auto" | "double" | "single" | "half" | "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32" | "int64" | "uint64" | "boolean" | "fixdt(1,16,0)" | "fixdt(1,16,2^0,0)" | "Enum: <class name>" | "<data type expression>"
Default: "Inherit: Inherit via back propagation" (for numeric and logical labels) | "Inherit: auto" (for nonnumeric labels)

Specify the lower value of the label output range that Simulink checks.

Simulink uses the minimum value to perform:

The Label data type Minimum parameter does not saturate or clip the actual label output signal. To do so, use the Saturation (Simulink) block instead.

Dependencies

You can specify this parameter only if the model specified by Select initial trained machine learning model uses numeric labels.

Programmatic Use

Block Parameter: LabelOutMin
Type: character vector
Values: "[]" | scalar
Default: "[]"

Specify the upper value of the label output range that Simulink checks.

Simulink uses the maximum value to perform:

The Label data type Maximum parameter does not saturate or clip the actual label output signal. To do so, use the Saturation (Simulink) block instead.

Dependencies

You can specify this parameter only if the model specified by Select initial trained machine learning model uses numeric labels.

Programmatic Use

Block Parameter: LabelOutMax
Type: character vector
Values: "[]" | scalar
Default: "[]"

Specify the data type for the score output. The type can be inherited, specified directly, or expressed as a data type object such as Simulink.NumericType.

When you select Inherit: auto, the block uses a rule that inherits a data type.

For more information about data types, see Control Data Types of Signals (Simulink).

Click the Show data type assistant button to display the Data Type Assistant, which helps you set the data type attributes. For more information, see Specify Data Types Using Data Type Assistant (Simulink).

Programmatic Use

Block Parameter: ScoreDataTypeStr
Type: character vector or string
Values: "Inherit: auto" | "double" | "single" | "half" | "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32" | "int64" | "uint64" | "boolean" | "fixdt(1,16,0)" | "fixdt(1,16,2^0,0)" | "<data type expression>"
Default: "Inherit: auto"

Specify the lower value of the score output range that Simulink checks.

Simulink uses the minimum value to perform:

The Score data type Minimum parameter does not saturate or clip the actual score output. To do so, use the Saturation (Simulink) block instead.

Programmatic Use

Block Parameter: ScoreOutMin
Type: character vector
Values: "[]" | scalar
Default: "[]"

Specify the upper value of the score output range that Simulink checks.

Simulink uses the maximum value to perform:

The Score data type Maximum parameter does not saturate or clip the actual score output. To do so, use the Saturation (Simulink) block instead.

Programmatic Use

Block Parameter: ScoreOutMax
Type: character vector
Values: "[]" | scalar
Default: "[]"

Specify the data type for the internal untransformed scores. The type can be inherited, specified directly, or expressed as a data type object such as Simulink.NumericType.

When you select Inherit: auto, the block uses a rule that inherits a data type.

For more information about data types, see Control Data Types of Signals (Simulink).

Click the Show data type assistant button to display the Data Type Assistant, which helps you set the data type attributes. For more information, see Specify Data Types Using Data Type Assistant (Simulink).

Dependencies

You can specify this parameter only if the model specified by Select initial trained machine learning model uses a score transformation other than "none" (default, same as "identity").

  • If the model uses no score transformations ("none" or "identity"), then you can specify the score data type by using Score data type.

  • If the model uses a score transformation other than "none" or "identity", then you can specify the data type of untransformed raw scores by using this parameter. To specify the data type of transformed scores, use Score data type.

You can change the score transformation option by specifying the ScoreTransform name-value argument during training, or by modifying the ScoreTransform property after training.

Programmatic Use

Block Parameter: RawScoreDataTypeStr
Type: character vector
Values: "Inherit: auto" | "double" | "single" | "half" | "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32" | "int64" | "uint64" | "boolean" | "fixdt(1,16,0)" | "fixdt(1,16,2^0,0)" | "<data type expression>"
Default: "Inherit: auto"

Specify the lower value of the untransformed score range that Simulink checks.

Simulink uses the minimum value to perform:

The Raw score data type Minimum parameter does not saturate or clip the actual untransformed score signal.

Programmatic Use

Block Parameter: RawScoreOutMin
Type: character vector
Values: "[]" | scalar
Default: "[]"

Specify the upper value of the untransformed score range that Simulink checks.

Simulink uses the maximum value to perform:

The Raw score data type Maximum parameter does not saturate or clip the actual untransformed score signal.

Programmatic Use

Block Parameter: RawScoreOutMax
Type: character vector
Values: "[]" | scalar
Default: "[]"

Specify the data type for the cost output. The type can be inherited, specified directly, or expressed as a data type object such as Simulink.NumericType.

When you select Inherit: auto, the block uses a rule that inherits a data type.

For more information about data types, see Control Data Types of Signals (Simulink).

Click the Show data type assistant button to display the Data Type Assistant, which helps you set the data type attributes. For more information, see Specify Data Types Using Data Type Assistant (Simulink).

Programmatic Use

Block Parameter: CostDataTypeStr
Type: character vector or string
Values: "Inherit: auto" | "double" | "single" | "half" | "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32" | "int64" | "uint64" | "boolean" | "fixdt(1,16,0)" | "fixdt(1,16,2^0,0)" | "<data type expression>"
Default: "Inherit: auto"

Specify the lower value of the cost output range that Simulink checks.

Simulink uses the minimum value to perform:

The Estimated cost data type Minimum parameter does not saturate or clip the actual cost signal. To do so, use the Saturation (Simulink) block instead.

Programmatic Use

Block Parameter: CostOutMin
Type: character vector
Values: "[]" | scalar
Default: "[]"

Specify the upper value of the cost output range that Simulink checks.

Simulink uses the maximum value to perform:

The Estimated cost data type Maximum parameter does not saturate or clip the actual cost signal. To do so, use the Saturation (Simulink) block instead.

Programmatic Use

Block Parameter: CostOutMax
Type: character vector
Values: "[]" | scalar
Default: "[]"

Additional Data Types

Specify the data type for the likelihood output. The type can be inherited, specified directly, or expressed as a data type object such as Simulink.NumericType.

When you select Inherit: auto, the block uses a rule that inherits a data type.

For more information about data types, see Control Data Types of Signals (Simulink).

Click the Show data type assistant button to display the Data Type Assistant, which helps you set the data type attributes. For more information, see Specify Data Types Using Data Type Assistant (Simulink).

Programmatic Use

Block Parameter: LikelihoodDataTypeStr
Type: character vector
Values: "Inherit: auto" | "double" | "single" | "half" | "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32" | "int64" | "uint64" | "boolean" | "fixdt(1,16,0)" | "fixdt(1,16,2^0,0)" | "<data type expression>"
Default: "Inherit: auto"

Specify the lower value of the likelihood output range that Simulink checks.

Simulink uses the minimum value to perform:

The Likelihood data type Minimum parameter does not saturate or clip the actual likelihood values. To do so, use the Saturation (Simulink) block instead.

Programmatic Use

Block Parameter: LikelihoodOutMin
Type: character vector
Values: "[]" | scalar
Default: "[]"

Specify the upper value of the likelihood output range that Simulink checks.

Simulink uses the maximum value to perform:

The Likelihood data type Maximum parameter does not saturate or clip the actual likelihood values. To do so, use the Saturation (Simulink) block instead.

Programmatic Use

Block Parameter: LikelihoodOutMax
Type: character vector
Values: "[]" | scalar
Default: "[]"

Specify the data type for posterior probabilities. The type can be inherited, specified directly, or expressed as a data type object such as Simulink.NumericType.

When you select Inherit: auto, the block uses a rule that inherits a data type.

For more information about data types, see Control Data Types of Signals (Simulink).

Click the Show data type assistant button to display the Data Type Assistant, which helps you set the data type attributes. For more information, see Specify Data Types Using Data Type Assistant (Simulink).

Programmatic Use

Block Parameter: PosteriorDataTypeStr
Type: character vector
Values: "Inherit: auto" | "double" | "single" | "half" | "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32" | "int64" | "uint64" | "boolean" | "fixdt(1,16,0)" | "fixdt(1,16,2^0,0)" | "<data type expression>"
Default: "Inherit: auto"

Specify the lower value of the posterior probability output range that Simulink checks.

Simulink uses the minimum value to perform:

The Posterior data type Minimum parameter does not saturate or clip the actual posterior probabilities. To do so, use the Saturation (Simulink) block instead.

Programmatic Use

Block Parameter: PosteriorOutMin
Type: character vector
Values: "[]" | scalar
Default: "[]"

Specify the upper value of the posterior probability output range that Simulink checks.

Simulink uses the maximum value to perform:

The Posterior data type Maximum parameter does not saturate or clip the actual posterior probabilities. To do so, use the Saturation (Simulink) block instead.

Programmatic Use

Block Parameter: PosteriorOutMax
Type: character vector
Values: "[]" | scalar
Default: "[]"

Block Characteristics

Data Types

Boolean | double | enumerated | fixed point | half | integer | single

Direct Feedthrough

yes

Multidimensional Signals

no

Variable-Size Signals

no

Zero-Crossing Detection

no

More About

expand all

Extended Capabilities

expand all

C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.

Fixed-Point Conversion
Design and simulate fixed-point systems using Fixed-Point Designer™.

Version History

Introduced in R2025a