www.gusucode.com > stats 源码程序 matlab案例代码 > stats/EstimateClassPosteriorProbabilitiesUsingAClassExample.m
%% Estimate Class Posterior Probabilities Using a Classification Tree %% % Load Fisher's iris data set. % Copyright 2015 The MathWorks, Inc. load fisheriris %% % Partition the data into training (50%) and validation (50%) sets. n = size(meas,1); rng(1) % For reproducibility idxTrn = false(n,1); idxTrn(randsample(n,round(0.5*n))) = true; % Training set logical indices idxVal = idxTrn == false; % Validation set logical indices %% % Grow a classification tree using the training set, and then view it. Mdl = fitctree(meas(idxTrn,:),species(idxTrn)); view(Mdl,'Mode','graph') %% % The resulting tree has four levels. %% % Estimate posterior probabilities for the test set using subtrees pruned % to levels 1 and 3. [~,Posterior] = predict(Mdl,meas(idxVal,:),'SubTrees',[1 3]); Mdl.ClassNames Posterior(randsample(size(Posterior,1),5),:,:),... % Display several posterior probabilities %% % The elements of |Posterior| are class posterior probabilities: % % * Rows correspond to observations in the validation set. % * Columns correspond to the classes as listed in % |Mdl.ClassNames|. % * Pages correspond to the subtrees. % %% % The subtree pruned to level 1 is more sure of its predictions than the % subtree pruned to level 3 (i.e., the root node).