4 %> @brief Class representing a Confusion matrix.
7 %> List of all
class labels matching the one of the classifier used. It is always used
as a reference at least
8 %> to renumber the classes of the @c
estimato dataset passed to @c do_record() .
11 %> Grouping of rows in the confusion matrix.
13 %> The matrix itself. The first column must represent rejected items
15 %> Whether or not the values are expressed in percentages.
17 %> =0. Whether to show the
"Rejected" column even
if there is no rejected item
18 flag_force_rejected = 0;
22 %> Whether there is any rejected item, i.e. a nonzero element in the first column. This is automatically calculated
24 %> WHether to show the rejected column. Calculated based on @ref flag_force_rejected and @ref flag_rejected
29 function flag =
get.flag_rejected(o)
30 flag = ~isempty(o.C) && any(o.C(:, 1) > 0);
33 function flag =
get.flag_show_rejected(o)
34 flag = o.flag_rejected || o.flag_force_rejected;
38 methods(Access=
protected)
39 %> Automatically detects
if confusion matrix has hits or percentages
40 function s = do_get_html(o)
41 s = o.get_html_table();
47 o.classtitle = 'Confusion Matrix';
50 function z = get_flag_sortable(o)
51 n1 = numel(o.collabels);
52 n2 = numel(o.rowlabels);
53 z = n1 == n2 && sum(strcmp(o.collabels, o.rowlabels)) == n1;
56 %> Automatically detects if confusion matrix has hits or percentages
57 function s = get_html_table(o)
59 s =
html_confusion(CC, o.rowlabels, o.collabels, o.flag_perc, o.flag_show_rejected);
63 %> This sorting is made to group rows/columns. It was used in one of the presentations to Unilever
64 %> @param no_levels Number of levels in index key
65 function o = sort(o, no_levels)
66 if ~o.get_flag_sortable()
67 irerror('Cannot sort, collabels and rowslabels are different!');
70 if ~exist('no_levels', 'var')
74 n = numel(o.collabels);
79 % Normalizes for keymaking
84 Ctemp = CC(:); Ctemp(Ctemp == 0) = [];
86 CC3 = (CC+eye(n))/ma; % This one just for the key values: diagonals have maximum weight!
87 CC = CC/ma; % This one to find the columns to be used
as keys
90 ii = 1:numel(o.collabels); %
#ok<NASGU>
91 iifinal = 1:numel(o.collabels); %#ok<NASGU>
97 dimmax = 2; %#ok<UNRCH>
100 % Finds a list of columns to use
as index keys
101 vk = zeros(1, no_levels);
102 CCtemp = CC; %#ok<NASGU>
105 [vv1, ii1] = max(CC(iitemp, iitemp), [], dimmax); %#ok<NASGU> % column maxima
106 [vv2, ii2] = max(vv1);
112 % Makes a key vector
for indexing
114 M = 10^(ceil(log10(ma))-log10(mi)+1);
116 for j = no_levels:-1:1
118 k = k+mult*CC3(:, vk(j))
';
120 k = k+mult*CC3(vk(j), :); %#ok<UNRCH>
125 [val, idxs] = sort(k, 'descend
');
127 o.collabels = o.collabels(idxs); % Somehow stupid since collabels and ...
128 o.rowlabels = o.rowlabels(idxs); % ... rowlabels need be the same, but anyway ...
135 %> Visualization. Draws figure with circles whose area are proportional to the percentuals of the corresponding
136 %> cells of the matrix
137 function o = draw_balls(o)
140 FULLDIAM = 0.8; % Diameter of an 100% ball
141 MINPERC_BALL = 0.02; % Minimum percentage to draw a ball
142 MINPERC_TEXT = 0.1; % Minimum percentage to write percentage text
146 [ni, nj] = size(Ccalc);
152 Cshow = round(Cshow*100)/100; % To make 2 decimal places only
155 % What to use for calculation (normalizes to 0-1)
156 Ccalc = normalize_rows(Ccalc);
158 % Makes header strings.
159 c = ['rejected
' o.collabels];
164 xlim = [0, nj+1-(1-o.flag_show_rejected)]; % x-limits
165 ylim = [0, (ni-1)*yspacing+2];
166 flag_same = compare(o.collabels, o.rowlabels);
172 for i = 1:ni % row loop
174 ypos = (i-1)*yspacing+1;
176 plot(xlim, ypos*[1, 1], 'k
', 'LineWidth
', scaled(3)); % Horizontal line
178 hh(end+1) = text(xlim(1)-0.1, ypos, o.rowlabels{i}, 'HorizontalAlignment
', 'right
'); % Descriptive text
179 hhtext1(end+1) = hh(end);
181 k = 0; % graphics column (whereas j is the matrix column)
182 for j = iif(o.flag_show_rejected, 1, 2):nj % column loop
186 hh(end+1) = text(k, -0.1, c{j}, 'HorizontalAlignment
', 'right
', 'Rotation
', 270);
187 hhtext2(end+1) = hh(end);
189 % plot(k*[1, 1], ylim, 'k
', 'LineWidth
', scaled(3)); % Vertical line
192 if Ccalc(i, j) > MINPERC_BALL
193 diam = sqrt(FULLDIAM*Ccalc(i, j));
194 pos = [k-diam/2, ypos-diam/2, diam, diam];
196 color_ = [1, 1, 1]*.3;
199 color_ = [0, 0.7, 0];
204 color_ = find_color(j-1);
206 rectangle('Position
', pos, 'LineStyle
', '-
', 'LineWidth
', scaled(2), 'FaceColor
', color_, 'Curvature
', [1, 1]);%, 'LineColor
', 'k
');
208 if Ccalc(i, j) >= MINPERC_TEXT
209 hh2(end+1) = text(k, ypos-diam/2, [sprintf('%g
', Cshow(i, j)), sperc], 'HorizontalAlignment
', 'center
', 'VerticalAlignment
', 'bottom
');
214 plot(xlim([1, 2, 2, 1, 1]), ylim([1, 1, 2, 2, 1]), 'Color
', [0, 0, 0], 'LineWidth
', scaled(2)); % Box
218 set(gca, 'YDir
', 'reverse
');
219 set(gca, 'XTick
', [], 'YTick
', []);
220 format_frank(gcf, 1, [hh, hh2]);
222 set(hh2, 'FontSize
', FONTSIZE*scaled(.75));
225 % Let's attempt to make all the ticks/(row/column titles)/(
class labels) to appear
226 maximize_window([], 1, .9); % Have to
do this, because the Extent
property below gives values relative to the gca(), and
this changes according to the size of the figure on screen
227 pause(0.2); % Has to wait until the window maximization takes place
230 % set(hhtext1(i),
'Unit',
'Normalized');
231 p =
get(hhtext1(i),
'Extent');
232 maxwid = max([maxwid, p(3)]);
235 for i = 1:numel(hhtext2)
236 % set(hhtext2(i),
'Unit',
'Normalized');
237 p =
get(hhtext2(i),
'Extent');
238 maxhei = max([maxhei, p(4)]);
241 xlim(1) = xlim(1)-maxwid-0.1;
242 ylim(1) = ylim(1)-maxhei-0.1;
244 set(gca,
'Xlim', xlim,
'YLim', ylim,
'position', [0.025, 0.025, 0.95, 0.95]);
248 % % set(hhtext2(i),
'Unit',
'Normalized');
249 % p =
get(hhtext2(i),
'Extent');
250 % maxhei = max([maxhei, p(4)]);
254 % totalwid = maxwid+1;
255 % totalhei = maxhei+1;
256 % x = maxwid/totalwid;
257 % y = maxhei/totalhei;
259 % p =
get(gca,
'Position');
264 %
if all(p([2, 4]) > 0)
265 % set(gca,
'Position', p);
function maximize_window(in h, in aspectratio, in normalizedsize)
function html_confusion(in CC, in rowlabels, in collabels, in flag_perc, in flag_rejected, in flag_color, in SS)
Dataset representing estimation.
Class representing a Confusion matrix.
Analysis Session (AS) base class.