www.gusucode.com > stats 源码程序 matlab案例代码 > stats/ReduceMemoryConsumptionOfSVMModelsExample.m
%% Reduce Memory Consumption of SVM Models % |predict| accepts compacted SVM models, and, for linear SVM models, does % not require the |Alpha|, |SupportVectors|, and |SupportVectorLabels| % properties to predict labels for new observations. If your training set % is large, consider compacting the SVM model, and then discarding the % stored support vectors and other related estimates. %% % Load the |ionosphere| data set. % Copyright 2015 The MathWorks, Inc. load ionosphere rng(1); % For reproducibility %% % Train an SVM model using default options. MdlSV = fitcsvm(X,Y); %% % |MdlSV| is a |ClassificationSVM| model containing nonempty values for its % |Alpha|, |SupportVectors|, and |SupportVectorLabels| properties. %% % Reduce the size of the SVM model by discarding the training data, support % vectors, and related estimates. CMdlSV = compact(MdlSV); % Discard training data CMdl = discardSupportVectors(CMdlSV); % Discard support vectors %% % |CMdl| is a |CompactClassificationSVM| model. %% % Compare the sizes of the SVM models |MdlSV| and |CMdl|. vars = whos('MdlSV','CMdl'); 100*(1 - vars(1).bytes/vars(2).bytes) %% % The compacted model consumes much less memory than the full model. %% % Predict the label for a random row of the training data using the more % efficient SVM model. idx = randsample(size(X,1),1) predictedLabel = predict(CMdl,X(idx,:)) trueLabel = Y(idx)