如何在 Matlab SVM 分类器中使用 crossval() 函数的输出创建混淆矩阵?

How to create a confusion matrix using the output of crossval() function in Matlab SVM classifier?

我必须在 Matlab 中测量 SVM 分类器的性能。混淆矩阵必须用作性能度量。然而,在Matlab的例子中,只能计算损失值。我找不到关于如何根据 crossval() 函数的结果创建混淆矩阵的信息。

SVMModel = fitcsvm(X,Y,'Standardize',true,'KernelFunction','RBF',...
    'KernelScale','auto');
CVSVMModel = crossval(SVMModel);
FirstModel = CVSVMModel.Trained{1};

执行此操作的示例 Matlab 代码。

% Example 3
% Compute the confusion matrix using stratified 10-fold cross validation:
% https://www.mathworks.com/help/stats/crossval.html

load('fisheriris');
% Class label
y = species;
% Measurments to classify with
X = meas;

% Class order
order = unique(y); % Order of the group labels

cp = cvpartition(y,'k',10); % Stratified cross-validation

f = @(xtr,ytr,xte,yte)confusionmat(yte,classify(xte,xtr,ytr),'order',order);

cfMat = crossval(f,X,y,'partition',cp);

cfMat = reshape(sum(cfMat),3,3)
% cfMat =
%     50     0     0
%      0    48     2
%      0     1    49
% cfMat is the summation of 10 confusion matrices from 10 test sets.`