Main Content

IncrementalClassificationNaiveBayes Fit

Fit incremental naive Bayes classification model

Since R2025a

  • IncrementalClassificationNaiveBayes Fit Block Icon

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

Description

The IncrementalClassificationNaiveBayes Fit block fits a configured incremental model for naive Bayes classification (incrementalClassificationNaiveBayes) to streaming data.

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 x receives a chunk of predictor data (observations), and the input port y receives a chunk of responses (labels) to which the model is fit. The output port mdl returns an updated incrementalClassificationNaiveBayes model. The optional input port w receives a chunk of observation weights and the optional input port reset resets the hyperparameters.

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

Chunk of predictor data to which the model is fit, specified as a numeric matrix. The orientation of the variables and observations is assumed to be rows, which indicates that the observations in the predictor data are oriented along the rows of x.

The length of the observation responses y and the number of observations in x must be equal; y(j) is the response of observation j (row or column) in x. The following restrictions apply:

  • The number of predictor variables in x must be equal to the NumPredictors property value of the initial model. If the number of predictor variables in the streaming data changes from NumPredictors, the block issues an error.

  • The IncrementalClassificationNaiveBayes Fit 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

Chunk of class labels to which the model is trained, specified as a numeric, logical, or enumerated vector.

  • The length of the observation responses y and the number of observations in x must be equal; y(j) is the response of observation j (row or column) in x.

  • Each label must correspond to one row of the array.

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

Chunk of observation weights, specified as a vector of positive values. The IncrementalClassificationNaiveBayes Fit block weights the observations in x with the corresponding values in w. The size of w must be equal to the number of observations in x.

Dependencies

To enable this port, select the check box for Add input port for observation weights on the Main tab of the Block Parameters dialog box.

Data Types: single | double

Since R2025a

Reset signal, specified as 0 (false) or 1 (true) or a numeric scalar. When the reset signal is a positive scalar (greater than 0), the block resets the learned parameters, if any, of the incremental learning model. If any hyperparameters of mdl are estimated during incremental training, those get reset as well. mdl.NumPredictors are always preserved.

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

Output

expand all

Updated parameters of the incremental learning model fit to streaming data, returned as a bus signal (see Composite Signals (Simulink)).

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 initial model must have following properties specified:

  • The NumPredictorsproperty which must be a positive integer scalar equal to the number of predictors in x.

  • The ClassNames property.

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 input port w for observation weights in the IncrementalClassificationNaiveBayes Fit block.

Programmatic Use

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

Since R2025a

Select the check box to include the input port reset for the reset signal in the IncrementalClassificationNaiveBayes Fit block.

Programmatic Use

Block Parameter: ShowInputReset
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 Fit 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 internal states in the mdl output bus signal. The type can be inherited, specified directly, or expressed as a data type object such as Simulink.NumericType.

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: StatesDataTypeStr
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 internal states range that Simulink checks.

Simulink uses the minimum value to perform:

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

Programmatic Use

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

Specify the upper value of the internal states range that Simulink checks.

Simulink uses the maximum value to perform:

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

Programmatic Use

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

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

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: PriorDataTypeStr
Type: character vector or string
Values: "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: "double"

Specify the lower value of the prior term range that Simulink checks.

Simulink uses the minimum value to perform:

The Prior data type Minimum parameter does not saturate or clip the actual prior term value. To do so, use the Saturation (Simulink) block instead.

Programmatic Use

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

Specify the upper value of the prior term range that Simulink checks.

Simulink uses the maximum value to perform:

The Prior data type Maximum parameter does not saturate or clip the actual prior term value. To do so, use the Saturation (Simulink) block instead.

Programmatic Use

Block Parameter: PriorOutMax
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

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