Help on Making an Accurate Confusion Matrix.
5 views (last 30 days)
Show older comments
Bradley Evans
on 27 Jun 2023
Edited: Bradley Evans
on 7 Jul 2023
I am going to submit the code below for a confusion matrix that I have made. (Okay, with some help from another.) When I run the code with my deep learning program, it seems that I get more misses than hits. In other words, I have a lot of entries for "U," which is an unknown variable, a "miss" in other words. I have 154 data files in which the computer is to learn and distinguish between Object A and Object B. What am I doing wrong? I do not think that the data is incorrect or needs to be massaged more. I think the error is in the confusion matrix that I have made. Help! I am confused about the confusion matrix. Can you have a look at the code and possibly help? Here is the code below. Many thanks!
%% Section 12: Create Confusion Matrix Chart
% From the MathWorks Help Center web-site under "confusionchart."
% Description
% confusionchart(trueLabels,predictedLabels) creates a confusion matrix chart from true labels trueLabels and
% predicted labels predictedLabels and returns a ConfusionMatrixChart object.
% The rows of the confusion matrix correspond to the true class and the columns correspond to the predicted class.
% Diagonal and off-diagonal cells correspond to correctly and incorrectly classified observations, respectively.
% Use cm to modify the confusion matrix chart after it is created.
% For a list of properties, see ConfusionMatrixChart Properties.
% Create Confusion Matrix Chart.
% Load a sample of predicted and true labels for a classification problem.
% trueLabels is the true labels for an image classification problem and
% predictedLabels is the predictions of a convolutional neural network.
trueLabels = dsTest.UnderlyingDatastores{1,2}.LabelData(:,2);
newTrueLabels = {};
for idx = 1:numel(trueLabels)
if trueLabels{idx} == 'Van'
newTrueLabels{idx} = 'V';
elseif trueLabels{idx} == 'Man'
newTrueLabels{idx} = 'M';
else
newTrueLabels{idx} = 'U'; % U means 'Unknown'. That is, it is a miss and neither 'Van' nor 'Man'.
end
end
predictedLabels = results.Labels;
newPredictedLabels = {};
for idx = 1:numel(predictedLabels)
if predictedLabels{idx} == 'Van'
newPredictedLabels{idx} = 'V';
elseif predictedLabels{idx} == 'Man'
newPredictedLabels{idx} = 'M';
else
newPredictedLabels{idx} = 'U'; % U means 'Unknown'. That is, it is a miss and neither 'Van' nor 'Man'.
end
end
% Create a confusion matrix chart.
figure(1)
cm = confusionchart(newTrueLabels,newPredictedLabels);
% Modify the appearance and behavior of the confusion matrix chart by changing property values.
% Add column and row summaries and a title.
% A column-normalized column summary displays the number of correctly and incorrectly classified observations
% for each predicted class as percentages of the number of observations of the corresponding predicted class.
% A row-normalized row summary displays the number of correctly and incorrectly classified observations
% for each true class as percentages of the number of observations of the corresponding true class.
cm.ColumnSummary = 'column-normalized';
cm.RowSummary = 'row-normalized';
cm.Title = 'Confusion Matrix for Man and Van Data';
disp("End of Section 12");
disp(" ");
0 Comments
Accepted Answer
ProblemSolver
on 28 Jun 2023
Based on the provided code, it seems that the issue lies in how you are assigning the 'Unknown' class ('U') to the misclassified samples. The problem arises from using the equality operator (`==`) for string comparison in MATLAB. In MATLAB, when comparing strings, you should use the `strcmp` function instead of the equality operator (`==`). The `strcmp` function compares two strings and returns a logical value indicating whether they are equal.
To fix the issue, modify the code where you compare the labels as follows:
if strcmp(trueLabels{idx}, 'Van')
newTrueLabels{idx} = 'V';
elseif strcmp(trueLabels{idx}, 'Man')
newTrueLabels{idx} = 'M';
else
newTrueLabels{idx} = 'U'; % U means 'Unknown'. That is, it is a miss and neither 'Van' nor 'Man'.
end
And similarly for the `predictedLabels` comparison:
if strcmp(predictedLabels{idx}, 'Van')
newPredictedLabels{idx} = 'V';
elseif strcmp(predictedLabels{idx}, 'Man')
newPredictedLabels{idx} = 'M';
else
newPredictedLabels{idx} = 'U'; % U means 'Unknown'. That is, it is a miss and neither 'Van' nor 'Man'.
end
By using `strcmp` instead of the equality operator, you ensure that the comparison between strings is done correctly, which should resolve the issue of having more 'Unknown' ('U') entries in your confusion matrix.
7 Comments
ProblemSolver
on 5 Jul 2023
Correct me, if my understanding diverges:
- you are providing the data points stored in a series of tables named T111, ...
- Each table contains the bounding box data points that are represented through {[]}.
- The second column appears to be reference of Classes variable. Is this variable defining a class or its label associated with each bounding box? Therefore, I need some clarification on the definition of the said Classes variable.
- Now, if you only one image, there is no need to add "0, 0, 0, 0" behind it unless there is a specific reason you want to do so.
- If there if no second image or bounding box, you can simply omit the data points for the second images, you don't to create a placeholder.
- Since I haven't really worked with the YOLO v2 deep learning subroutine, I cannot comment on it, unless I details such as how your want your Confusion Matrix (or Confusion Chart) be structured. Along with it I need to know how these images are stored or processed as it is unclear.
If it is possible, show a sample illustration for what you are exactly looking for to better help you.
More Answers (2)
See Also
Categories
Find more on Classification Trees 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!