Documentation

loadCompactModel

从保存模型中重建紧凑型分类或回归模型,以生成代码

要生成用于分类或回归的C代码,您必须有一个MATLAB®CODER™license.

Syntax

compactmdl = loadCompactmodel(文件名)

描述

example

CompactMdl= loadCompactModel(filename)重建紧凑的分类或回归模型CompactMdlfrom the saved model stored in the MATLAB formatted binary file (MAT-file)filename。你必须创建filenameusingSaveCompactModel

例子

全部收缩

加载电离层data set.

load电离层

Train an SVM classification model using the entire data set. Specify to standardize the data.

MDL= fitcsvm(X,Y,“标准化”,真正的)
MDL= ClassificationSVM ResponseName: 'Y' CategoricalPredictors: [] ClassNames: {'b' 'g'} ScoreTransform: 'none' NumObservations: 351 Alpha: [90×1 double] Bias: -0.1343 KernelParameters: [1×1 struct] Mu: [1×34 double] Sigma: [1×34 double] BoxConstraints: [351×1 double] ConvergenceInfo: [1×1 struct] IsSupportVector: [351×1 logical] Solver: 'SMO'

MDLis a分类vmmodel.

将SVM分类模型保存到文件'SVMIonosphere.mat'

SaveCompactModel(Mdl,'SVMIonosphere');

'SVMIonosphere.mat'出现在您目前的工作目录中。SaveCompactModel通过删除预测不需要的属性(例如训练数据)来减少模型的内存足迹。然后,SaveCompactModel保存一个特征的结构数组MDL'SVMIonosphere.mat'

加载structure array in'SVMIonosphere.mat'to the Workspace.

compactmdl = loadcompactmodel('SVMIonosphere')
CompactMdl = classreg.learning.classif.CompactClassificationSVM ResponseName: 'Y' CategoricalPredictors: [] ClassNames: {'b' 'g'} ScoreTransform: 'none' Alpha: [90×1 double] Bias: -0.1343 KernelParameters: [1×1 struct] Mu: [1×34 double] Sigma: [1×34 double] SupportVectors: [90×34 double] SupportVectorLabels: [90×1 double]

CompactMdlis acompactClassificationvm设备进行预测的模型。

Declare a function that classifies new observations using a trained classification model. This example requires a MATLAB® Coder™ license.

加载Fisher的虹膜数据集。

load渔业

培训由SVM二进制学习者组成的ECOC模型。使用SVM模板,标准化数据,保留30%的培训数据,并指定高斯内核。

t = templatesvm(“标准化”,true,'kernelfunction','gaussian');CVMDL= fitcecoc(meas,species,“学习者”,t,'Holdout',0.30);

CVMDLis a分类部门存储等的模型:

  • A compact ECOC model, whichfitcecoctrained using the training set.

  • 数据分区对象,指定如何fitcecocrandomly split the data into training and holdout sets.

Extract the compact ECOC classification model and the holdout set indices using dot notation. Identify the holdout observations.

compactmdl = cvmdl.Trained {1};cvp = cvmdl.partition;idxtest = test(CVP);MEATHO = MEAS(IDXTEST,:);

CompactMdlis aCompactClassificationECOCmodel equipped to classify new observations.

Save the compact ECOC classification model to the fileECOCIris.mat

SaveCompactModel(CompactMdl,'ecociris');

Declare a function in your current working folder called分类那:

  • Accepts iris-flower measurements commensurate with测量并返回预测标签

  • 加载紧凑的ECOC分类模型

  • Passes the loaded model and iris-flower measurements to预测

功能label = classifyIrises(X)%#codegen百分比使用ECOC模型对虹膜物种进行分类% CLASSIFYIRISES classifies the iris-flower measurements in X%在文件ecociris.mat中使用紧凑型ECOC模型,然后% returns class labels in label.compactmdl = loadcompactmodel('ecociris');label = predition(compactmdl,x);end

The%#codegencompilation directive indicates that the MATLAB® code is intended for code generation.

Generate a MEX function from分类。Because C uses static typing,codegenmust determine the properties of all variables in MATLAB® files at compile time. To ensure that the MEX function can use the same input, specify the holdout observations as arguments to the function using the'-args'选项。

codegen分类-args{measHO}

MEX文件classifyiries_mex.mexw64生成您当前的工作目录。文件扩展名取决于您的平台。

Compare labels predicted using预测at the command line,分类, 和classifyIrises_mex

label1 = predition(compactmdl,measho);label2 = classifyirise(measho);label3 = classifyirises_mex(meadho);comp12 = cellfun(@strcmp,label1,label2);Comp23 = CellFun(@strcmp,label2,label3);class12 = sum(comp12)== numel(label1)colle23 = sum(comp23)== numel(label1)
agree12 = logical 1 agree23 = logical 1

标签预测所有三种方式都是相同的。

输入参数

全部收缩

MAT文件名称包含代表紧凑型分类或回归模型的结构数组,该模型指定为字符向量。你必须创建filenameusingSaveCompactModel

loadCompactModel重建紧凑的分类或回归模型stored infilename在编译时。有关支持金宝app的紧凑分类或回归模型,请参见MDL输入参数。

如果filename没有扩展名(即,没有延期的时间),然后loadCompactModelappends。mat

如果filenamedoes not include a full path, thenloadCompactModelloads from the present working directory.

例子:'CompactMdl'

Output Arguments

全部收缩

Compact classification or regression model, returned as分类线,CompactClassificationECOC,紧凑型classificationEnsemble,compactClassificationvm,compactClassificationTree,CompactLinearModel,紧凑型地线模型,或CompactregressionTreemodel object.

CompactMdl必须是一个编译时间常数,也就是说,您在加载它后不能更改它loadCompactModel

Extended Capabilities

Introduced in R2016b

这个话题有帮助吗?