www.gusucode.com > stats 源码程序 matlab案例代码 > stats/SelectNaiveBayesClassifierFeaturesByComparingTestSampleEExample.m
%% Select Naive Bayes Classifier Features by Comparing Test Sample Edges % The classifier edge measures the average of the classifier margins. One % way to perform feature selection is to compare test sample edges from % multiple models. Based solely on this criterion, the classifier with the % highest edge is the best classifier. %% % Load Fisher's iris data set. % Copyright 2015 The MathWorks, Inc. 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 two 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}; %% % |FCVMdl| and |PCVMdl| 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 edge for each classifier. fullEdge = edge(FCMdl,XTest,YTest) partEdge = edge(PCMdl,XTest(:,3:4),YTest) %% % The test-sample edges of the classifiers are nearly the same. However, % the model trained using two predictors (|PCMdl|) is less complex.