www.gusucode.com > stats 源码程序 matlab案例代码 > stats/EstimateTheTestSampleWeightedMarginMeanOfNaiveBayesClassExample.m
%% Estimate the Test Sample Weighted Margin Mean of Naive Bayes Classifiers %% % Load Fisher's iris data set. % Copyright 2015 The MathWorks, Inc. load fisheriris X = meas; % Predictors Y = species; % Response rng(1); %% % Suppose that the setosa iris measurements are lower quality because % they were measured with an older technology. One way to incorporate this % is to weigh the setosa iris measurements less than the % other observations. % % Define a weight vector that weighs the better quality observations twice % the other observations. n = size(X,1); idx = strcmp(Y,'setosa'); weights = ones(size(X,1),1); weights(idx) = 0.5; %% % Train a naive Bayes classifier. Specify the weighting scheme and 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,'Weights',weights,'Holdout',0.30,... 'ClassNames',{'setosa','versicolor','virginica'}); CMdl = CVMdl.Trained{1}; % Extract trained, compact classifier testInds = test(CVMdl.Partition); % Extract the test indices XTest = X(testInds,:); YTest = Y(testInds); wTest = weights(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 weighted edge using the weighting scheme. e = edge(CMdl,XTest,YTest,'Weights',wTest) %% % The test sample weighted average margin is approximately 0.79. This % indicates that, on average, the test sample difference between the % estimated posterior probability for the predicted class and the posterior % probability for the class with the next lowest posterior probability is % approximately 0.79. This indicates that the classifier labels with high % confidence.