main content

compact naive bayes classifier for multiclass classification -凯发k8网页登录

compact naive bayes classifier for multiclass classification

description

compactclassificationnaivebayes is a compact version of the naive bayes classifier. the compact classifier does not include the data used for training the naive bayes classifier. therefore, you cannot perform some tasks, such as cross-validation, using the compact classifier. use a compact naive bayes classifier for tasks such as predicting the labels of the data.

creation

create a compactclassificationnaivebayes model from a full, trained classifier by using .

properties

predictor properties

this property is read-only.

predictor names, specified as a cell array of character vectors. the order of the elements in predictornames corresponds to the order in which the predictor names appear in the training data x.

this property is read-only.

expanded predictor names, specified as a cell array of character vectors.

if the model uses dummy variable encoding for categorical variables, then expandedpredictornames includes the names that describe the expanded variables. otherwise, expandedpredictornames is the same as predictornames.

this property is read-only.

categorical predictor indices, specified as a vector of positive integers. categoricalpredictors contains index values indicating that the corresponding predictors are categorical. the index values are between 1 and p, where p is the number of predictors used to train the model. if none of the predictors are categorical, then this property is empty ([]).

data types: single | double

this property is read-only.

multivariate multinomial levels, specified as a cell array. the length of categoricallevels is equal to the number of predictors (size(x,2)).

the cells of categoricallevels correspond to predictors that you specify as 'mvmn' during training, that is, they have a multivariate multinomial distribution. cells that do not correspond to a multivariate multinomial distribution are empty ([]).

if predictor j is multivariate multinomial, then categoricallevels{j} is a list of all distinct values of predictor j in the sample. nans are removed from unique(x(:,j)).

predictor distribution properties

this property is read-only.

predictor distributions, specified as a character vector or cell array of character vectors. fitcnb uses the predictor distributions to model the predictors. this table lists the available distributions.

valuedescription
'kernel'kernel smoothing density estimate
'mn'multinomial distribution. if you specify mn, then 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. for details, see .
'mvmn'multivariate multinomial distribution. for details, see .
'normal'normal (gaussian) distribution

if distributionnames is a 1-by-p cell array of character vectors, then fitcnb models the feature j using the distribution in element j of the cell array.

example: 'mn'

example: {'kernel','normal','kernel'}

data types: char | string | cell

this property is read-only.

distribution parameter estimates, specified as a cell array. distributionparameters is a k-by-d cell array, where cell (k,d) contains the distribution parameter estimates for instances of predictor d in class k. the order of the rows corresponds to the order of the classes in the property classnames, and the order of the predictors corresponds to the order of the columns of x.

if class k has no observations for predictor j, then the distribution{k,j} is empty ([]).

the elements of distributionparameters depend on the distributions of the predictors. this table describes the values in distributionparameters{k,j}.

distribution of predictor jvalue of cell array for predictor j and class k
kernela model. display properties using cell indexing and dot notation. for example, to display the estimated bandwidth of the kernel density for predictor 2 in the third class, use mdl.distributionparameters{3,2}.bandwidth.
mna scalar representing the probability that token j appears in class k. for details, see .
mvmna numeric vector containing the probabilities for each possible level of predictor j in class k. the software orders the probabilities by the sorted order of all unique levels of predictor j (stored in the property categoricallevels). for more details, see .
normala 2-by-1 numeric vector. the first element is the sample mean and the second element is the sample standard deviation. for more details, see

this property is read-only.

kernel smoother type, specified as the name of a kernel or a cell array of kernel names. the length of kernel is equal to the number of predictors (size(x,2)). kernel{j} corresponds to predictor j and contains a character vector describing the type of kernel smoother. if a cell is empty ([]), then fitcnb did not fit a kernel distribution to the corresponding predictor.

this table describes the supported kernel smoother types. i{u} denotes the indicator function.

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}

example: 'box'

example: {'epanechnikov','normal'}

data types: char | string | cell

this property is read-only.

kernel smoother density support, specified as a cell array. the length of support is equal to the number of predictors (size(x,2)). the cells represent the regions to which fitcnb applies the kernel density. if a cell is empty ([]), then fitcnb did not fit a kernel distribution to the corresponding predictor.

this table describes the supported options.

valuedescription
1-by-2 numeric row vectorthe density support applies to the specified bounds, for example [l,u], where l and u are the finite lower and upper bounds, respectively.
'positive'the density support applies to all positive real values.
'unbounded'the density support applies to all real values.

this property is read-only.

kernel smoother window width, specified as a numeric matrix. width is a k-by-p matrix, where k is the number of classes in the data, and p is the number of predictors (size(x,2)).

width(k,j) is the kernel smoother window width for the kernel smoothing density of predictor j within class k. nans in column j indicate that fitcnb did not fit predictor j using a kernel density.

response properties

this property is read-only.

unique class names used in the training model, specified as a categorical or character array, logical or numeric vector, or cell array of character vectors.

classnames has the same data type as y, and has k elements (or rows) for character arrays. (the software treats string arrays as cell arrays of character vectors.)

data types: categorical | char | string | logical | double | cell

this property is read-only.

response variable name, specified as a character vector.

data types: char | string

training properties

prior probabilities, specified as a numeric vector. the order of the elements in prior corresponds to the elements of mdl.classnames.

fitcnb normalizes the prior probabilities you set using the 'prior' name-value pair argument, so that sum(prior) = 1.

the value of prior does not affect the best-fitting model. therefore, you can reset prior after training mdl using dot notation.

example: mdl.prior = [0.2 0.8]

data types: double | single

classifier properties

misclassification cost, specified as a numeric square matrix, where cost(i,j) is the cost of classifying a point into class j if its true class is i. the rows correspond to the true class and the columns correspond to the predicted class. the order of the rows and columns of cost corresponds to the order of the classes in classnames.

the misclassification cost matrix must have zeros on the diagonal.

the value of cost does not influence training. you can reset cost after training mdl using dot notation.

example: mdl.cost = [0 0.5 ; 1 0]

data types: double | single

classification score transformation, specified as a character vector or function handle. this table summarizes the available character vectors.

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

for a matlab® function or a function you define, use its function handle for the score transformation. the function handle must accept a matrix (the original scores) and return a matrix of the same size (the transformed scores).

example: mdl.scoretransform = 'logit'

data types: char | string | function handle

object functions

compare accuracies of two classification models using new data
classification edge for naive bayes classifier
limelocal interpretable model-agnostic explanations (lime)
log unconditional probability density for naive bayes classifier
classification loss for naive bayes classifier
classification margins for naive bayes classifier
partialdependencecompute partial dependence
plotpartialdependencecreate partial dependence plot (pdp) and individual conditional expectation (ice) plots
classify observations using naive bayes classifier
shapleyshapley values

examples

reduce the size of a full naive bayes classifier by removing the training data. full naive bayes classifiers hold the training data. you can use a compact naive bayes classifier to improve memory efficiency.

load the ionosphere data set. remove the first two predictors for stability.

load ionosphere
x = x(:,3:end);

train a naive bayes classifier using the predictors x and class labels y. a recommended practice is to specify the class names. fitcnb assumes that each predictor is conditionally and normally distributed.

mdl = fitcnb(x,y,'classnames',{'b','g'})
mdl = 
  classificationnaivebayes
              responsename: 'y'
     categoricalpredictors: []
                classnames: {'b'  'g'}
            scoretransform: 'none'
           numobservations: 351
         distributionnames: {1x32 cell}
    distributionparameters: {2x32 cell}
  properties, methods

mdl is a trained classificationnaivebayes classifier.

reduce the size of the naive bayes classifier.

cmdl = compact(mdl)
cmdl = 
  compactclassificationnaivebayes
              responsename: 'y'
     categoricalpredictors: []
                classnames: {'b'  'g'}
            scoretransform: 'none'
         distributionnames: {1x32 cell}
    distributionparameters: {2x32 cell}
  properties, methods

cmdl is a trained compactclassificationnaivebayes classifier.

display the amount of memory used by each classifier.

whos('mdl','cmdl')
  name      size             bytes  class                                                        attributes
  cmdl      1x1              15060  classreg.learning.classif.compactclassificationnaivebayes              
  mdl       1x1             111190  classificationnaivebayes                                               

the full naive bayes classifier (mdl) is more than seven times larger than the compact naive bayes classifier (cmdl).

to label new observations efficiently, you can remove mdl from the matlab® workspace, and then pass cmdl and new predictor values to .

train and cross-validate a naive bayes classifier. fitcnb implements 10-fold cross-validation by default. then, estimate the cross-validated classification error.

load the ionosphere data set. remove the first two predictors for stability.

load ionosphere
x = x(:,3:end);
rng('default')  % for reproducibility

train and cross-validate a naive bayes classifier using the predictors x and class labels y. a recommended practice is to specify the class names. fitcnb assumes that each predictor is conditionally and normally distributed.

cvmdl = fitcnb(x,y,'classnames',{'b','g'},'crossval','on')
cvmdl = 
  classificationpartitionedmodel
    crossvalidatedmodel: 'naivebayes'
         predictornames: {'x1'  'x2'  'x3'  'x4'  'x5'  'x6'  'x7'  'x8'  'x9'  'x10'  'x11'  'x12'  'x13'  'x14'  'x15'  'x16'  'x17'  'x18'  'x19'  'x20'  'x21'  'x22'  'x23'  'x24'  'x25'  'x26'  'x27'  'x28'  'x29'  'x30'  'x31'  'x32'}
           responsename: 'y'
        numobservations: 351
                  kfold: 10
              partition: [1x1 cvpartition]
             classnames: {'b'  'g'}
         scoretransform: 'none'
  properties, methods

cvmdl is a classificationpartitionedmodel cross-validated, naive bayes classifier. alternatively, you can cross-validate a trained classificationnaivebayes model by passing it to .

display the first training fold of cvmdl using dot notation.

cvmdl.trained{1}
ans = 
  compactclassificationnaivebayes
              responsename: 'y'
     categoricalpredictors: []
                classnames: {'b'  'g'}
            scoretransform: 'none'
         distributionnames: {1x32 cell}
    distributionparameters: {2x32 cell}
  properties, methods

each fold is a compactclassificationnaivebayes model trained on 90% of the data.

full and compact naive bayes models are not used for predicting on new data. instead, use them to estimate the generalization error by passing cvmdl to kfoldloss.

generror = kfoldloss(cvmdl)
generror = 0.1852

on average, the generalization error is approximately 19%.

you can specify a different conditional distribution for the predictors, or tune the conditional distribution parameters to reduce the generalization error.

more about

algorithms

references

[1] hastie, trevor, robert tibshirani, and jerome friedman. the elements of statistical learning: data mining, inference, and prediction. 2nd ed. springer series in statistics. new york, ny: springer, 2009. https://doi.org/10.1007/978-0-387-84858-7.

[2] manning, christopher d., prabhakar raghavan, and hinrich schütze. introduction to information retrieval, ny: cambridge university press, 2008.

extended capabilities

version history

introduced in r2014b

see also

| | |

topics

    网站地图