www.gusucode.com > stats 源码程序 matlab案例代码 > stats/SelectBestRegularizedModelsECOCExample.m

    %% Select Best Regularized Models
%%
% Load the NLP data set.
load nlpdata
%%
% |X| is a sparse matrix of predictor data, and |Y| is a categorical vector
% of class labels. 
%%
% Create a set of 11 logarithmically-spaced regularization strengths from
% $10^{-8}$ through $10^{-1}$.
Lambda = logspace(-8,-1,11);  
%%
% Create a linear-classification-model template that specifies to solve the
% objective function using SpaRSA, and use lasso penalties with the
% strengths specified in |Lambda|.
t = templateLinear('Solver','sparsa','Regularization','lasso',...
    'Lambda',Lambda);
%%
% Hold out 30% of the data for testing.  Identify the test-sample indices.
rng(1); % For reproducibility
cvp = cvpartition(Y,'Holdout',0.30);
idxTest = test(cvp);
%%
% Train an ECOC model composed of linear classification models.  For
% quicker execution time, orient the predictor data so that individual
% observations correspond to columns.
X = X';
CVMdl = fitcecoc(X,Y,'Learners',t,'ObservationsIn','columns','CVPartition',cvp);
Mdl = CVMdl.Trained{1};
numel(Mdl.BinaryLearners{1}.Lambda)
%%
% |Mdl| is a |CompactClassificationECOC| model object.  Because |Lambda| is
% an 11-dimensional vector of regularization strengths, you can think of
% |Mdl| as eleven trained models, one for each regularization strength.
%%
% Estimate the test-sample misclassification rates for each regularized
% model.
ce = loss(Mdl,X(:,idxTest),Y(idxTest),'ObservationsIn','columns');
%%
% Plot the misclassification rates with respect to regularization strength
% on the log scale.
figure;
plot(log10(Lambda),log10(ce),'-o')
ylabel('log_{10} misclassification rates')
xlabel('log_{10} Lambda')
[~,minCEIdx] = min(ce);
minLambda = Lambda(minCEIdx);
hold on
plot(log10(minLambda),log10(ce(minCEIdx)),'ro');
hold off
%%
% Several values of |Lambda| yield similarly small classification error
% values. Higher values of lambda lead to predictor variable sparsity,
% which is a good quality of a classifier.
%%
% Select the four models with the regularization strengths that occur
% around where the classification error starts increasing.
idx = 7:10;
MdlFinal = selectModels(Mdl,idx)
LambdaFinal = MdlFinal.BinaryLearners{1}.Lambda
%%
% |MdlFinal| is a |CompactClassificationECOC| model object.  You can think
% of it as three models trained using the four regularization strengths in
% |LambaFinal|.