## Copyright (C) 2020 Stefano Guidoni ## ## This program is free software; you can redistribute it and/or modify it under ## the terms of the GNU General Public License as published by the Free Software ## Foundation; either version 3 of the License, or (at your option) any later ## version. ## ## This program is distributed in the hope that it will be useful, but WITHOUT ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more ## details. ## ## You should have received a copy of the GNU General Public License along with ## this program; if not, see . ## -*- texinfo -*- ## @deftypefn {} {} confusionchart (@var{trueLabels}, @var{predictedLabels}) ## @deftypefnx {} {} confusionchart (@var{m}) ## @deftypefnx {} {} confusionchart (@var{m}, @var{classLabels}) ## @deftypefnx {} {} confusionchart (@var{parent}, @dots{}) ## @deftypefnx {} {} confusionchart (@dots{}, @var{prop}, @var{val}, @dots{}) ## @deftypefnx {} {@var{cm} =} confusionchart (@dots{}) ## ## Display a chart of a confusion matrix. ## ## @var{trueLabels} and @var{predictedLabels} are the two vectors of values, ## which are used to compute the confusion matrix. ## Otherwise an already computed confusion matrix, @var{m}, can be given. ## ## @var{classLabels} is an array of labels, i.e. the list of the class names. ## ## If the first argument is a handle to a @code{figure} or to a @code{uipanel}, ## then the confusion matrix chart is displayed inside that object. ## ## Optional property/value pairs are passed directly to the underlying objects, ## e.g. @qcode{"xlabel"}, @qcode{"ylabel"}, @qcode{"title"}, @qcode{"fontname"}, ## @qcode{"fontsize"} etc. ## ## The optional return value @var{cm} is a @code{ConfusionMatrixChart} object. ## ## @end deftypefn ## ## @seealso{confusionmat, sortClasses} ## Author: Stefano Guidoni function cm = confusionchart (varargin) ## check the input parameters if (nargin < 1) print_usage (); endif p_i = 1; if ishghandle (varargin{p_i}) ## parameter is a parent figure handle_type = get (varargin{p_i}, "type"); if strcmp (handle_type, "figure") h = figure (varargin{p_i}); hax = axes ('parent', h); elseif strcmp (handle_type, "uipanel") h = varargin{p_i}; hax = axes ('parent', varargin{p_i}); else ## MATLAB compatibility: on MATLAB are also available Tab objects, ## TiledChartLayout objects, GridLayout objects error ("confusionchart: invalid handle to parent object"); endif p_i++; else h = figure (); hax = axes ('parent', h); endif if ismatrix (varargin{p_i}) && rows (varargin{p_i}) == ... columns (varargin{p_i}) ## parameter is a confusion matrix conmat = varargin{p_i}; p_i++; if p_i <= nargin && ((isvector (varargin{p_i}) && ... length (varargin{p_i}) == rows (conmat)) || ... (ischar ( varargin{p_i}) && rows (varargin{p_i}) == rows (conmat)) ... || iscellstr (varargin{p_i})) ## parameter is an array of labels labarr = varargin{p_i}; if isrow (labarr) labarr = vec (labarr); endif p_i++; else labarr = [1 : rows (conmat)]'; endif elseif isvector (varargin{p_i}) ## parameter must be a group for confusionmat [conmat, labarr] = confusionmat (varargin{p_i}, varargin{p_i + 1}); p_i = p_i + 2; else error ("confusionchart: invalid argument"); endif ## remaining parameters are stored i = p_i; args = {}; while (i <= nargin) args{end + 1} = varargin{i++}; endwhile ## prepare the labels if !iscellstr (labarr) if !ischar (labarr) labarr = num2str (labarr); endif labarr = cellstr (labarr); endif ## MATLAB compatibility: labels are sorted [labarr, I] = sort (labarr); conmat = conmat(I, :); conmat = conmat(:, I); cm = ConfusionMatrixChart (hax, conmat, labarr, args); endfunction ## Demonstration using the confusion matrix example from ## R.Bonnin, "Machine Learning for Developers", pp. 55-56 %!demo 1 %! Yt = [8 5 6 8 5 3 1 6 4 2 5 3 1 4]'; %! Yp = [8 5 6 8 5 2 3 4 4 5 5 7 2 6]'; %! confusionchart (Yt, Yp, "Title", ... %! "Demonstration with summaries","Normalization",... %! "absolute","ColumnSummary", "column-normalized","RowSummary",... %! "row-normalized") ## example: confusion matrix and class labels %!demo 2 %! Yt = {'Positive', 'Positive', 'Positive', 'Negative', 'Negative' }; %! Yp = {'Positive', 'Positive', 'Negative', 'Negative', 'Negative' }; %! cm = confusionmat ( Yt, Yp ); %! confusionchart ( cm, { 'Positive', 'Negative' } ); ## example: editing the properties of an existing ConfusionMatrixChart object %!demo 3 %! Yt = {'Positive', 'Positive', 'Positive', 'Negative', 'Negative' }; %! Yp = {'Positive', 'Positive', 'Negative', 'Negative', 'Negative' }; %! cm = confusionchart ( Yt, Yp ); %! cm.Title = "This is an example with a green diagonal"; %! cm.DiagonalColor = [0.4660, 0.6740, 0.1880]; ## example: drawing the chart inside a uipanel %!demo 4 %! h = uipanel (); %! Yt = {'Positive', 'Positive', 'Positive', 'Negative', 'Negative' }; %! Yp = {'Positive', 'Positive', 'Negative', 'Negative', 'Negative' }; %! cm = confusionchart ( h, Yt, Yp );