matlab templatesvm,Support vector machine template

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'}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值