フィルターのクリア

how to plot confusion matrix from confusion matrix?

12 ビュー (過去 30 日間)
Hassan Ashraf
Hassan Ashraf 2019 年 12 月 5 日
回答済み: Taylor 2024 年 1 月 24 日
I have a confusion matrix, in numbers. I want to plot the percentage classification accuracies. I found some code from fileExchnage, but its calculating percentages wrongly. Please help to figure it out.
load confmat.mat
numlabels = size(confmat, 1); % number of labels
% calculate the percentage accuracies
confpercent = 100*confmat./repmat(sum(confmat, 1),numlabels,1);
% plotting the colors
imagesc(confpercent);
ylabel('Output Class'); xlabel('Target Class');
% set the colormap
colormap(flipud(gray));
% Create strings from the matrix values and remove spaces
textStrings = num2str(confpercent(:), '%.1f%\n%d\n');
textStrings = strtrim(cellstr(textStrings));
% Create x and y coordinates for the strings and plot them
[x,y] = meshgrid(1:numlabels);
hStrings = text(x(:),y(:),textStrings(:), ...
'HorizontalAlignment','center', 'FontSize', 38);
% Get the middle value of the color range
midValue = mean(get(gca,'CLim'));
% Choose white or black for the text color of the strings so
% they can be easily seen over the background color
textColors = repmat(confpercent(:) > midValue,1,3);
set(hStrings,{'Color'},num2cell(textColors,2));
  2 件のコメント
Akira Agata
Akira Agata 2019 年 12 月 5 日
If you have 'Deep Learning Toolbox' or 'Statistics and Machine Learning Toolbox', you can use confusionchart function to plot the chart easily.
Malathi Kunnudaiyan
Malathi Kunnudaiyan 2024 年 1 月 24 日
size(confmat, 1)
Why are we using 1 as an order here (confmat, 1)? What does it mean?

サインインしてコメントする。

回答 (1 件)

Taylor
Taylor 2024 年 1 月 24 日
load confmat.mat
% Calculate the percentage accuracy for each class
percentageAccuracies = (diag(confmat)' ./ sum(confmat, 1)) * 100;
% Plot the percentage accuracies
figure;
bar(1:11, percentageAccuracies);
xlabel('Class');
ylabel('Percentage Accuracy');
title('Classification Accuracies');
xticks(1:11); % Set x-axis ticks to represent each class
xticklabels({'Class 1', 'Class 2', 'Class 3', 'Class 4', 'Class 5', 'Class 6', 'Class 7', 'Class 8', 'Class 9', 'Class 10', 'Class 11'});
grid on; % Add grid for better readability

カテゴリ

Help Center および File ExchangeColormaps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by