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

4 views (last 30 days)
I had implemented a simple face recognition code. how to calculate its accuracy? and how to plot the confusion matrix .
clc
clear
% Load Image dataset
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
trainingFeatures(featureCount,:) = extractHOGFeatures(read(training(i),j));
% imshow(read(training(i),j));
%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
queryImage = read(test(person),j);
queryFeatures = extractHOGFeatures(queryImage);
actualLabel = predict(faceClassifier,queryFeatures) %actuallabel
C=test(person).Description; % predictedlabel
predictedLabel= cellstr(C) %converting into cell array
end
end

Answers (1)

Hitham
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

Community Treasure Hunt

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

Start Hunting!