When you train an ECOC model with linear SVM binary learners, fitcecoc empties the Alpha, SupportVectorLabels, and SupportVectors properties of the binary learners by default. You can choose instead to retain the support vectors and related values, and then discard them from the model later.
Load Fisher's iris data set.
load fisheriris
rng(1); % For reproducibility
Train an ECOC model using the entire data set. Specify retaining the support vectors by passing in the appropriate SVM template.
t = templateSVM('SaveSupportVectors',true);
MdlSV = fitcecoc(meas,species,'Learners',t);
MdlSV is a trained ClassificationECOC model with linear SVM binary learners. By default, fitcecoc implements a one-versus-one coding design, which requires three binary learners for three-class learning.
Access the estimated α (alpha) values using dot notation.
alpha = cell(3,1);
alpha{1} = MdlSV.BinaryLearners{1}.Alpha;
alpha{2} = MdlSV.BinaryLearners{2}.Alpha;
alpha{3} = MdlSV.BinaryLearners{3}.Alpha;
alpha
alpha=3×1 cell array
{ 3x1 double}
{ 3x1 double}
{23x1 double}
alpha is a 3-by-1 cell array that stores the estimated values of α.
Discard the support vectors and related values from the ECOC model.
Mdl = discardSupportVectors(MdlSV);
Mdl is similar to MdlSV, except that the Alpha, SupportVectorLabels, and SupportVectors properties of all the linear SVM binary learners are empty ([]).
areAllEmpty = @(x)isempty([x.Alpha x.SupportVectors x.SupportVectorLabels]);
cellfun(areAllEmpty,Mdl.BinaryLearners)
ans = 3x1 logical array
1
1
1
Compare the sizes of the two ECOC models.
vars = whos('Mdl','MdlSV');
100*(1 - vars(1).bytes/vars(2).bytes)
ans = 4.7075
Mdl is about 5% smaller than MdlSV.
Reduce your memory usage by compacting Mdl and then clearing Mdl and MdlSV from the workspace.
CompactMdl = compact(Mdl);
clear Mdl MdlSV;
Predict the label for a random row of the training data using the more efficient SVM model.
idx = randsample(size(meas,1),1)
idx = 63
predictedLabel = predict(CompactMdl,meas(idx,:))
predictedLabel = 1x1 cell array
{'versicolor'}
trueLabel = species(idx)
trueLabel = 1x1 cell array
{'versicolor'}