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

2 次查看(过去 30 天)
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

回答(1 个)

Hitham
Hitham 2020-9-25
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

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by