Search code examples
matlabsvmcross-validationconfusion-matrix

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


I have to measure the performance of SVM classifier in Matlab. Confusion matrix must be used as the performance measure. However, in the examples in Matlab, only loss value can be calculated. I could find no info about how to create a confusion matrix from the result of crossval() function.

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

Solution

  • Example Matlab code to do this.

    % 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.`