Documentation Center

  • Trial Software
  • Product Updates


Class: NaiveBayes

Create Naive Bayes classifier object by fitting training data


nb =, class)
nb =, 'param1',val1, 'param2',val2, ...)

    Note:   fit will be removed in a future release. Use fitNaiveBayes instead.


nb =, class) builds a NaiveBayes classifier object nb. training is an N-by-D numeric matrix of training data. Rows of training correspond to observations; columns correspond to features. class is a classing variable for training taking K distinct levels. Each element of class defines which class the corresponding row of training belongs to. training and class must have the same number of rows.

nb =, 'param1',val1, 'param2',val2, ...) specifies one or more of the following name/value pairs:

  • 'Distribution' – a string or a 1-by-D cell vector of strings, specifying which distributions fit uses to model the data. If the value is a string, fit models all the features using one type of distribution. fit can also model different features using different types of distributions. If the value is a cell vector, its jth element specifies the distribution fit uses for the jth feature. The available types of distributions are:

    'normal' (default)Normal (Gaussian) distribution.
    'kernel'Kernel smoothing density estimate.
    'mvmn'Multivariate multinomial distribution for discrete data. fit assumes each individual feature follows a multinomial model within a class. The parameters for a feature include the probabilities of all possible values that the corresponding feature can take.
    'mn'Multinomial distribution for classifying the count-based data such as the bag-of-tokens model. In the bag-of-tokens model, the value of the jth feature is the number of occurrences of the jth token in this observation, so it must be a non-negative integer. When 'mn' is used, fit considers each observation as multiple trials of a multinomial distribution, and considers each occurrence of a token as one trial. The number of categories (bins) in this multinomial model is the number of distinct tokens, i.e., the number of columns of training.

  • 'Prior' – The prior probabilities for the classes, specified as one of the following:

    'empirical' (default)fit estimates the prior probabilities from the relative frequencies of the classes in training.
    'uniform'The prior probabilities are equal for all classes.
    vectorA numeric vector of length K specifying the prior probabilities in the class order of class.
    structureA structure S containing class levels and their prior probabilities. S must have two fields:
    • S.prob: A numeric vector of prior probabilities.

    • S.class: A vector of the same type as class, containing unique class levels indicating the class for the corresponding element of prob. S.class must contain all the K levels in class. It can also contain classes that do not appear in class. This can be useful if training is a subset of a larger training set. fit ignores any classes that appear in S.class but not in class.

    If the prior probabilities don't sum to one, fit will normalize them.

  • 'KSWidth' – The bandwidth of the kernel smoothing window. The default is to select a default bandwidth automatically for each combination of feature and class, using a value that is optimal for a Gaussian distribution. You can specify the value as one of the following:

    scalarWidth for all features in all classes.
    row vector1-by-D vector where the jth element is the bandwidth for the jth feature in all classes.
    column vectorK-by-1 vector where the ith element specifies the bandwidth for all features in the ith class. K represents the number of class levels.
    matrixK-by-D matrix M where M(i,j) specifies the bandwidth for the jth feature in the ith class.
    structureA structure S containing class levels and their bandwidths. S must have two fields:
    • S.width – A numeric array of bandwidths specified as a row vector, or a matrix with D columns.

    • S.class – A vector of the same type as class, containing unique class levels indicating the class for the corresponding row of width.

  • 'KSSupport' – The regions where the density can be applied. It can be a string, a two-element vector as shown below, or a 1-by-D cell array of these values:

    'unbounded' (default)The density can extend over the whole real line.
    'positive'The density is restricted to positive values.
    [L,U]A two-element vector specifying the finite lower bound L and upper bound U for the support of the density.

  • 'KSType' – The type of kernel smoother to use. It can be a string or a 1-by-D cell array of strings. Each string can be 'normal' (default), 'box', 'triangle', or 'epanechnikov'.

How To

Was this topic helpful?