Contenuto principale

classificationNaiveBayesComponent

Pipeline component for multiclass classification using naive Bayes model

Since R2026a

    Description

    classificationNaiveBayesComponent is a pipeline component that creates a naive Bayes model for multiclass classification. The pipeline component uses the functionality of the fitcnb function during the learn phase to train the naive Bayes classification model. The component uses the functionality of the predict and loss functions during the run phase to perform classification.

    Creation

    Description

    component = classificationNaiveBayesComponent creates a pipeline component for a naive Bayes classification model.

    example

    component = classificationNaiveBayesComponent(Name=Value) sets writable Properties using one or more name-value arguments. For example, you can specify the data distribution, the cost of misclassification, and the kernel smoother type.

    Properties

    expand all

    Structural Parameters

    The software sets structural parameters when you create the component. You cannot modify structural parameters after creating the component.

    This property is read-only after the component is created.

    Observation weights flag, specified as 0 (false) or 1 (true). If UseWeights is true, the component adds a third input "Weights" to the Inputs component property, and a third input tag 3 to the InputTags component property.

    Example: c = classificationNaiveBayesComponent(UseWeights=1)

    Data Types: logical

    Learn Parameters

    The software sets learn parameters when you create the component. You can modify learn parameters using dot notation any time before you use the learn object function. Any unset learn parameters use the corresponding default values.

    Misclassification cost, specified as a square matrix or a structure.

    • If Cost is a square matrix, Cost(i,j) is the cost of classifying a point into class j if its true class is i.

    • If Cost is a structure S, it has two fields: S.ClassificationCosts, which contains the cost matrix; and S.ClassNames, which contains the group names and defines the class order of the rows and columns of the cost matrix.

    The default is Cost(i,j)=1 if i~=j, and Cost(i,j)=0 if i=j.

    Example: c = classificationNaiveBayesComponent(Cost=[0 1; 2 0])

    Example: c.Cost = [0 2; 1 0]

    Data Types: single | double | struct

    Data distributions used by the component to model the data, specified as a string scalar, character vector, string array, or cell array of character vectors consisting of values from this table.

    ValueDescription
    "kernel"Kernel smoothing density estimate
    "mn"Multinomial distribution. If you specify "mn", all features are components of a multinomial distribution. Therefore, you cannot include "mn" as an element of a string array or a cell array of character vectors.
    "mvmn"Multivariate multinomial distribution
    "normal"Normal (Gaussian) distribution

    If you specify a character vector or string scalar, the component models all features using that distribution. If you specify a string array or cell array of character vectors, the component models feature j using the distribution in element j of DistributionNames.

    By default, the component sets the distribution of all categorical predictors to "mvmn". Otherwise, the default distribution is "normal".

    Example: c = classificationNaiveBayesComponent(DistributionNames="mn")

    Example: c.DistributionNames = "normal"

    Data Types: char | string | cell

    Kernel smoother type, specified as a string scalar, character vector, string array, or cell array of character vectors consisting of values from this table.

    ValueKernelFormula
    "box"Box (uniform)

    f(x)=0.5I{|x|1}

    "epanechnikov"Epanechnikov

    f(x)=0.75(1x2)I{|x|1}

    "normal"Gaussian

    f(x)=12πexp(0.5x2)

    "triangle"Triangular

    f(x)=(1|x|)I{|x|1}

    If you specify a character vector or string scalar, the component trains the classifier for all features using that kernel smoother type. If you specify a string array or cell array of character vectors, the component uses the kernel smoother type in element j of Kernel to train the classifier for feature j. The component ignores elements of Kernel that do not correspond to a predictor whose distribution is "kernel".

    This property is valid only when DistributionNames contains the value "kernel".

    Example: c = classificationNaiveBayesComponent(Kernel="epanechnikov")

    Example: c.Kernel = "triangle"

    Data Types: char | string | cell

    Prior probabilities for each class, specified as a value in this table.

    ValueDescription
    "empirical"The class prior probabilities are the class relative frequencies. The class relative frequencies are determined by the second data argument of learn.
    "uniform"All class prior probabilities are equal to 1/K, where K is the number of classes.
    numeric vectorA numeric vector with one value for each class. Each element is a class prior probability. The component normalizes the elements such that they sum to 1.
    structure

    A structure S with two fields:

    • S.ClassNames contains a list of the class names.

    • S.ClassProbs contains a vector of corresponding prior probabilities. The component normalizes the elements such that they sum to 1.

    If you set UseWeights to true, the component renormalizes the weights to add up to the value of the prior probability in the respective class.

    Example: c = classificationNaiveBayesComponent(Prior="uniform")

    Example: c.Prior = "empirical"

    Data Types: single | double | char | string | struct

    Kernel smoothing density support, specified as a character vector, string scalar, string array, cell array, or numeric row vector. The component applies the kernel smoothing density to the region specified by the options in this table.

    ValueDescription
    1-by-2 numeric row vector[L,U], where L is the finite lower bound for the density support and U is the finite upper bound
    "positive"The density support is all positive real values.
    "unbounded"The density support is all real values.

    If you specify a character vector, string scalar, or numeric row vector, the component trains the classifier using that kernel support for all features. If you specify a string array or cell array, the component uses the kernel support in element j of Support to train the classifier for feature j. The component ignores elements of Support that do not correspond to a predictor whose distribution is "kernel".

    This property is valid only when DistributionNames contains the value "kernel".

    Example: c = classificationNaiveBayesComponent(Support={[-10,20],"unbounded"})

    Example: c.Support = "positive"

    Data Types: double | char | string | cell

    Width of the kernel smoothing window, specified as a matrix of numeric values, numeric column vector, numeric row vector, or numeric scalar.

    Suppose you have K class levels and P predictors. This table summarizes the available options for setting Width.

    ValueDescription
    K-by-P matrix of numeric valuesElement (k,j) specifies the width for predictor j in class k.
    K-by-1 numeric column vectorElement k specifies the width for all predictors in class k.
    1-by-P numeric row vectorElement j specifies the width in all class levels for predictor j.
    numeric scalarThis value specifies the bandwidth for all features in all classes.

    By default, the component automatically selects a default width for each combination of predictor and class by using a value that is optimal for a Gaussian distribution. If Width contains NaNs, the component selects widths for the elements containing NaNs.

    This property is valid only when DistributionNames contains the value "kernel".

    Example: c = classificationNaiveBayesComponent(Width=[NaN NaN])

    Example: c.Width = 0.1

    Data Types: single | double

    Run Parameters

    The software sets run parameters when you create the component. You can modify the run parameters using dot notation at any time. Any unset run parameters use the corresponding default values.

    Loss function, specified as a built-in loss function name or a function handle.

    This table lists the available built-in loss functions.

    ValueDescription
    "binodeviance"Binomial deviance
    "classifcost"Observed misclassification cost
    "classiferror"Misclassified rate in decimal
    "exponential"Exponential loss
    "hinge"Hinge loss
    "logit"Logistic loss
    "mincost"Minimal expected misclassification cost (for classification scores that are posterior probabilities)
    "quadratic"Quadratic loss

    To specify a custom loss function, use function handle notation. For more information on custom loss functions, see LossFun.

    Example: c = classificationNaiveBayesComponent(LossFun="classifcost")

    Example: c.LossFun = "hinge"

    Data Types: char | string | function_handle

    Score transformation, specified as a built-in function name or a function handle.

    This table summarizes the available built-in score transform functions.

    ValueDescription
    "doublelogit"1/(1 + e–2x)
    "invlogit"log(x / (1 – x))
    "ismax"Sets the score for the class with the largest score to 1, and sets the scores for all other classes to 0
    "logit"1/(1 + ex)
    "none" or "identity"x (no transformation)
    "sign"–1 for x < 0
    0 for x = 0
    1 for x > 0
    "symmetric"2x – 1
    "symmetricismax"Sets the score for the class with the largest score to 1, and sets the scores for all other classes to –1
    "symmetriclogit"2/(1 + ex) – 1

    To specify a custom score transform function, use function handle notation. The function must accept a matrix containing the original scores and return a matrix of the same size containing the transformed scores.

    Example: c = classificationNaiveBayesComponent(ScoreTransform="logit")

    Example: c.ScoreTransform = "symmetric"

    Data Types: char | string | function_handle

    Component Properties

    The software sets component properties when you create the component. You can modify the component properties (excluding HasLearnables and HasLearned) at any time. You cannot modify the HasLearnables and HasLearned properties directly.

    Component identifier, specified as a character vector or string scalar.

    Example: c = classificationNaiveBayesComponent(Name="NaiveBayes")

    Example: c.Name = "NaiveBayesClassifier"

    Data Types: char | string

    Names of the input ports, specified as a character vector, string array, or cell array of character vectors. If UseWeights is true, the component adds the input port "Weights" to Inputs.

    Example: c = classificationNaiveBayesComponent(Inputs=["X","Y"])

    Example: c.Inputs = ["In1","In2"]

    Data Types: char | string | cell

    Names of the output ports, specified as a character vector, string array, or cell array of character vectors.

    Example: c = classificationNaiveBayesComponent(Outputs=["Class","ClassScore","LossVal"])

    Example: c.Outputs = ["X","Y","Z"]

    Data Types: char | string | cell

    Tags that enable the automatic connection of the component inputs with other components or pipelines, specified as a nonnegative integer vector. If you specify InputTags, the number of tags must match the number of inputs in Inputs. If UseWeights is true, the component adds a third input tag to InputTags.

    Example: c = classificationNaiveBayesComponent(InputTags=[1 0])

    Example: c.InputTags = [0 1]

    Data Types: single | double

    Tags that enable the automatic connection of the component outputs with other components or pipelines, specified as a nonnegative integer vector. If you specify OutputTags, the number of tags must match the number of outputs in Outputs.

    Example: c = classificationNaiveBayesComponent(OutputTags=[1 0 4])

    Example: c.OutputTags=[1 2 0]

    Data Types: single | double

    This property is read-only.

    Indicator for learnables, returned as 1 (true). A value of 1 indicates that the component contains Learnables.

    Data Types: logical

    This property is read-only.

    Indicator showing the learning status of the component, returned as 0 (false) or 1 (true). A value of 1 indicates that the learn object function has been applied to the component, and the Learnables are nonempty.

    Data Types: logical

    Learnables

    The software sets learnables when you use the learn object function. You cannot modify learnables directly.

    This property is read-only.

    Trained model, returned as a CompactClassificationNaiveBayes model object.

    Object Functions

    learnInitialize and evaluate pipeline or component
    runExecute pipeline or component for inference after learning
    resetReset pipeline or component
    seriesConnect components in series to create pipeline
    parallelConnect components or pipelines in parallel to create pipeline
    viewView diagram of pipeline inputs, outputs, components, and connections

    Examples

    collapse all

    Create a classificationNaiveBayesComponent pipeline component.

    component = classificationNaiveBayesComponent
    component = 
      classificationNaiveBayesComponent with properties:
    
                Name: "ClassificationNaiveBayes"
              Inputs: ["Predictors"    "Response"]
           InputTags: [1 2]
             Outputs: ["Predictions"    "Scores"    "Loss"]
          OutputTags: [1 0 0]
    
       
    Learnables (HasLearned = false)
        TrainedModel: []
    
       
    Structural Parameters (locked)
          UseWeights: 0
    
    
    Show all parameters
    

    component is a classificationNaiveBayesComponent object that contains one learnable, TrainedModel. This property remains empty until you pass data to the component during the learn phase.

    To model the data using a kernel smoothing density estimate, set the DistributionNames property of the component to "kernel".

    component.DistributionNames = "kernel";

    Read the fisheriris data set into a table. Store the predictor and response data in the tables X and Y, respectively.

    fisheriris = readtable("fisheriris.csv");
    X = fisheriris(:,1:end-1);
    Y = fisheriris(:,end);

    Use the learn function to train the classificationNaiveBayesComponent object using the entire data set.

    component = learn(component,X,Y)
    component = 
      classificationNaiveBayesComponent with properties:
    
                     Name: "ClassificationNaiveBayes"
                   Inputs: ["Predictors"    "Response"]
                InputTags: [1 2]
                  Outputs: ["Predictions"    "Scores"    "Loss"]
               OutputTags: [1 0 0]
    
       
    Learnables (HasLearned = true)
             TrainedModel: [1×1 classreg.learning.classif.CompactClassificationNaiveBayes]
    
       
    Structural Parameters (locked)
               UseWeights: 0
    
       
    Learn Parameters (locked)
        DistributionNames: 'kernel'
    
    
    Show all parameters
    

    Note that the HasLearned property is set to true, which indicates that the software trained the naive Bayes model TrainedModel. You can use component to classify new data using the run function.

    Version History

    Introduced in R2026a

    See Also

    | |