margin

Classification margins for naive Bayes classifiers

Description

m = margin(Mdl,tbl,ResponseVarName) returns the classification margins (m) for the trained naive Bayes classifier Mdl using the predictor data in table tbl and the class labels in tbl.ResponseVarName.

m = margin(Mdl,tbl,Y) returns the classification margins (m) for the trained naive Bayes classifier Mdl using the predictor data in table tbl and the class labels in vector Y.

example

m = margin(Mdl,X,Y) returns the classification margins (m) for the trained naive Bayes classifier Mdl using the predictor data X and class labels Y.

Input Arguments

expand all

Naive Bayes classifier, specified as a ClassificationNaiveBayes model or CompactClassificationNaiveBayes model returned by fitcnb or compact, respectively.

Sample data, specified as a table. Each row of tbl corresponds to one observation, and each column corresponds to one predictor variable. Optionally, tbl can contain additional columns for the response variable and observation weights. tbl must contain all the predictors used to train Mdl. Multi-column variables and cell arrays other than cell arrays of character vectors are not allowed.

If you trained Mdl using sample data contained in a table, then the input data for this method must also be in a table.

Data Types: table

Response variable name, specified as the name of a variable in tbl.

You must specify ResponseVarName as a character vector or string scalar. For example, if the response variable y is stored as tbl.y, then specify it as 'y'. Otherwise, the software treats all columns of tbl, including y, as predictors when training the model.

The response variable must be a categorical, character, or string array, logical or numeric vector, or cell array of character vectors. If the response variable is a character array, then each element must correspond to one row of the array.

Data Types: char | string

Predictor data, specified as a numeric matrix.

Each row of X corresponds to one observation (also known as an instance or example), and each column corresponds to one variable (also known as a feature). The variables making up the columns of X should be the same as the variables that trained Mdl.

The length of Y and the number of rows of X must be equal.

Data Types: double | single

Class labels, specified as a categorical, character, or string array, logical or numeric vector, or cell array of character vectors. Y must be the same as the data type of Mdl.ClassNames. (The software treats string arrays as cell arrays of character vectors.)

The length of Y and the number of rows of tbl or X must be equal.

Data Types: categorical | char | string | logical | single | double | cell

Output Arguments

expand all

Classification margins, returned as a numeric vector.

m has the same length equal to size(X,1). Each entry of m is the classification margin of the corresponding observation (row) of X and element of Y.

Examples

expand all

Load Fisher's iris data set.

load fisheriris
X = meas;    % Predictors
Y = species; % Response
rng(1);

Train a naive Bayes classifier. Specify a 30% holdout sample for testing. It is good practice to specify the class order. Assume that each predictor is conditionally normally distributed given its label.

CVMdl = fitcnb(X,Y,'Holdout',0.30,...
    'ClassNames',{'setosa','versicolor','virginica'});
CMdl = CVMdl.Trained{1}; ...
    % Extract the trained, compact classifier
testInds = test(CVMdl.Partition);   % Extract the test indices
XTest = X(testInds,:);
YTest = Y(testInds);

CVMdl is a ClassificationPartitionedModel classifier. It contains the property Trained, which is a 1-by-1 cell array holding a CompactClassificationNaiveBayes classifier that the software trained using the training set.

Estimate the test sample classification margins. Display the distribution of the margins using a boxplot.

m = margin(CMdl,XTest,YTest);

figure;
boxplot(m);
title 'Distribution of the Test-Sample Margins';

An observation margin is the observed true class score minus the maximum false class score among all scores in the respective class. Classifiers that yield relatively large margins are desirable.

The classifier margins measure, for each observation, the difference between the true class observed score and the maximal false class score for a particular class. One way to perform feature selection is to compare test sample margins from multiple models. Based solely on this criterion, the model with the highest margins is the best model.

Load Fisher's iris data set.

load fisheriris
X = meas;    % Predictors
Y = species; % Response
rng(1);

Partition the data set into training and test sets. Specify a 30% holdout sample for testing.

Partition = cvpartition(Y,'Holdout',0.30);
testInds = test(Partition); % Indices for the test set
XTest = X(testInds,:);
YTest = Y(testInds);

Partition defines the data set partition.

Define these two data sets:

  • fullX contains all predictors.

  • partX contains the last 2 predictors.

fullX = X;
partX = X(:,3:4);

Train naive Bayes classifiers for each predictor set. Specify the partition definition.

FCVMdl = fitcnb(fullX,Y,'CVPartition',Partition);
PCVMdl = fitcnb(partX,Y,'CVPartition',Partition);
FCMdl = FCVMdl.Trained{1};
PCMdl = PCVMdl.Trained{1};

FullCVMdl and PartCVMdl are ClassificationPartitionedModel classifiers. They contain the property Trained, which is a 1-by-1 cell array holding a CompactClassificationNaiveBayes classifier that the software trained using the training set.

Estimate the test sample margins for each classifier. Display the distributions of the margins for each model using boxplots.

fullM = margin(FCMdl,XTest,YTest);
partM = margin(PCMdl,XTest(:,3:4),YTest);

figure;
boxplot([fullM partM],'Labels',{'All Predictors','Two Predictors'})
h = gca;
h.YLim = [0.98 1.01]; % Modify axis to see boxes.
title 'Boxplots of Test-Sample Margins';

The margins have a similar distribution, but PCMdl is less complex.

More About

expand all

Extended Capabilities