# How to calculate the accuracy of the classifier ? and show the confusion matrix.

28 views (last 30 days)
Munshida P on 28 Jan 2020
Answered: Hitham on 25 Sep 2020
I had implemented a simple face recognition code. how to calculate its accuracy? and how to plot the confusion matrix .
clc
clear
faceDatabase = imageSet('facedatabaseatt','recursive');
%splitting into training and testing sets
[training,test] = partition(faceDatabase,[0.8 0.2]);
% Extract HOG Features for training set
featureCount = 1;
for i=1:size(training,2)
for j = 1:training(i).Count
%pause(0.0011);
trainingLabel{featureCount} = training(i).Description;
featureCount = featureCount + 1;
end
personIndex{i} = training(i).Description;
end
% Create 40 class classifier
faceClassifier = fitcknn(trainingFeatures,trainingLabel);
%testing
for person=1:40
for j = 1:test(person).Count
queryFeatures = extractHOGFeatures(queryImage);
actualLabel = predict(faceClassifier,queryFeatures) %actuallabel
C=test(person).Description; % predictedlabel
predictedLabel= cellstr(C) %converting into cell array
end
end

Hitham on 25 Sep 2020
confusionmat computes the Confusion matrix as.
[cm,order] = confusionmat(actualLabel,predictedLabel);
Or, you can use my function.
function [c_matrix]=confusionmat1(actual,predict)
% It computes confusion matrix
classList=unique(actual);
N=length(classList);
cm = zeros(N,N);
for j=1:length(actual)
posClassGT = strmatch(actual{j}, classList, 'exact');
posClass = strmatch(predict{j}, classList, 'exact');
cm(posClassGT,posClass) = cm(posClassGT,posClass) + 1;
end
end

### Categories

Find more on Classification Ensembles in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by