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

18 ビュー (過去 30 日間)
Munshida P
Munshida P 2020 年 1 月 28 日
回答済み: Hitham 2020 年 9 月 25 日
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

Community Treasure Hunt

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

Start Hunting!

Translated by