margin

朴素贝叶斯分类器分类利润率

Description

m= margin(Mdl,tbl,ResponseVarName)returns theclassification margins(m) for the trained naive Bayes classifierMdlusing the predictor data in tabletbland the class labels intbl.ResponseVarName.

m= margin(Mdl,tbl,Y)returns the classification margins (m) for the trained naive Bayes classifierMdlusing the predictor data in tabletbland the class labels in vectorY.

example

m= margin(Mdl,X,Y)returns the classification margins (m) for the trained naive Bayes classifierMdlusing the predictor dataXand class labelsY.

Input Arguments

expand all

Naive Bayes classifier, specified as aClassificationNaiveBayesmodel orCompactClassificationNaiveBayesmodel returned byfitcnborcompact, respectively.

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

If you trainedMdlusing sample data contained in atable, 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 intbl.

You must specifyResponseVarNameas a character vector or string scalar. For example, if the response variableyis stored astbl.y, then specify it as'y'. Otherwise, the software treats all columns oftbl, includingy, 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 ofXcorresponds 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 ofXshould be the same as the variables that trainedMdl.

The length ofYand the number of rows ofXmust 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.Ymust be the same as the data type ofMdl.ClassNames.(The software treats string arrays as cell arrays of character vectors.)

The length ofYand the number of rows oftblorXmust be equal.

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

Output Arguments

expand all

Classification margins, returned as a numeric vector.

mhas the same length equal tosize(X,1). Each entry ofmis the classification margin of the corresponding observation (row) ofXand element ofY.

Examples

expand all

Load Fisher's iris data set.

loadfisheririsX = meas;% PredictorsY = species;% Responserng(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 classifiertestInds = test(CVMdl.Partition);% Extract the test indicesXTest = X(testInds,:); YTest = Y(testInds);

CVMdlis aClassificationPartitionedModelclassifier. It contains the propertyTrained, which is a 1-by-1 cell array holding aCompactClassificationNaiveBayesclassifier 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.

loadfisheririsX = meas;% PredictorsY = species;% Responserng(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 setXTest = X(testInds,:); YTest = Y(testInds);

Partition defines the data set partition.

Define these two data sets:

  • fullXcontains all predictors.

  • partXcontains 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};

FullCVMdlandPartCVMdlareClassificationPartitionedModelclassifiers. They contain the propertyTrained, which is a 1-by-1 cell array holding aCompactClassificationNaiveBayesclassifier 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, butPCMdlis less complex.

More About

expand all

Extended Capabilities