How to avoid displaying zero-values in confusion matrix

送分小仙女□ 提交于 2019-12-20 04:54:32

问题


I plotted a confusion matrix in Matlab using the code from this link.

However whenever there is a zero on the cell it is still shown. How can I eliminate the printing of 0.00's on the cells?

Sample of my confusion matrix


回答1:


After you removed all spaces, find '0.00' and substitute it with spaces again

idx = find(strcmp(textStrings(:), '0.00'));
textStrings(idx) = {'   '};

The complete code will then be:

mat = rand(5);           %# A 5-by-5 matrix of random values from 0 to 1
mat(3,3) = 0;            %# To illustrate
mat(5,2) = 0;            %# To illustrate
imagesc(mat);            %# Create a colored plot of the matrix values
colormap(flipud(gray));  %# Change the colormap to gray (so higher values are
                         %#   black and lower values are white)

textStrings = num2str(mat(:),'%0.2f');  %# Create strings from the matrix values
textStrings = strtrim(cellstr(textStrings));  %# Remove any space padding

%% ## New code: ###
idx = find(strcmp(textStrings(:), '0.00'));
textStrings(idx) = {'   '};
%% ################

[x,y] = meshgrid(1:5);   %# Create x and y coordinates for the strings
hStrings = text(x(:),y(:),textStrings(:),...      %# Plot the strings
                'HorizontalAlignment','center');
midValue = mean(get(gca,'CLim'));  %# Get the middle value of the color range
textColors = repmat(mat(:) > midValue,1,3);  %# Choose white or black for the
                                             %#   text color of the strings so
                                             %#   they can be easily seen over
                                             %#   the background color
set(hStrings,{'Color'},num2cell(textColors,2));  %# Change the text colors

set(gca,'XTick',1:5,...                         %# Change the axes tick marks
        'XTickLabel',{'A','B','C','D','E'},...  %#   and tick labels
        'YTick',1:5,...
        'YTickLabel',{'A','B','C','D','E'},...
        'TickLength',[0 0]);

This gives:




回答2:


does this work - do a loop over i and j (spatial dimensions) after textStrings is defined and before it is converted to cell, and set

textStrings(i,j,1:4)='    ';

depending on if mat(i,j) is really close to 0.00 using an if-else statement



来源:https://stackoverflow.com/questions/21215352/how-to-avoid-displaying-zero-values-in-confusion-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!