sortClasses
Sort classes of confusion matrix chart
Syntax
Description
Examples
Sort Classes in a Fixed Order
Load a sample of predicted and true labels for a classification problem. trueLabels
are the true labels for an image classification problem and predictedLabels
are the predictions of a convolutional neural network. Create a confusion matrix chart.
load('Cifar10Labels.mat','trueLabels','predictedLabels'); figure cm = confusionchart(trueLabels,predictedLabels);
Reorder the classes of the confusion matrix chart so that the classes are in a fixed order.
sortClasses(cm, ... ["cat" "dog" "horse" "deer" "bird" "frog", ... "airplane" "ship" "automobile" "truck"])
Sort Classes by Precision or Recall
Load a sample of predicted and true labels for a classification problem. trueLabels
are the true labels for an image classification problem and predictedLabels
are the predictions of a convolutional neural network. Create a confusion matrix chart with column and row summaries
load('Cifar10Labels.mat','trueLabels','predictedLabels'); figure cm = confusionchart(trueLabels,predictedLabels, ... 'ColumnSummary','column-normalized', ... 'RowSummary','row-normalized');
To sort the classes of the confusion matrix by class-wise recall (true positive rate), normalize the cell values across each row, that is, by the number of observations that have the same true class. Sort the classes by the corresponding diagonal cell values and reset the normalization of the cell values. The classes are now sorted such that the percentages in the blue cells in the row summaries to the right are decreasing.
cm.Normalization = 'row-normalized'; sortClasses(cm,'descending-diagonal'); cm.Normalization = 'absolute';
To sort the classes by class-wise precision (positive predictive value), normalize the cell values across each column, that is, by the number of observations that have the same predicted class. Sort the classes by the corresponding diagonal cell values and reset the normalization of the cell values. The classes are now sorted such that the percentages in the blue cells in the column summaries at the bottom are decreasing.
cm.Normalization = 'column-normalized'; sortClasses(cm,'descending-diagonal'); cm.Normalization = 'absolute';
Input Arguments
cm
— Confusion matrix chart
ConfusionMatrixChart
object
Confusion matrix chart, specified as a ConfusionMatrixChart
object. To create a confusion matrix chart, use confusionchart
,
order
— Order in which to sort classes
'auto'
| 'ascending-diagonal'
| 'descending-diagonal'
| 'cluster'
| array
Order in which to sort the classes of the confusion matrix chart, specified as one of these values:
'auto'
— Sorts the classes into their natural order as defined by thesort
function. For example, if the class labels of the confusion matrix chart are a string vector, then sort alphabetically. If the class labels are an ordinal categorical vector, then use the order of the class labels.'ascending-diagonal'
— Sort the classes so that the values along the diagonal of the confusion matrix increase from top left to bottom right.'descending-diagonal'
— Sort the classes so that the values along the diagonal of the confusion matrix decrease from top left to bottom right.'cluster'
(Requires Statistics and Machine Learning Toolbox™) — Sort the classes to cluster similar classes. You can customize clustering by using thepdist
(Statistics and Machine Learning Toolbox),linkage
(Statistics and Machine Learning Toolbox), andoptimalleaforder
(Statistics and Machine Learning Toolbox) functions. For details, see Sort Classes to Cluster Similar Classes (Statistics and Machine Learning Toolbox).Array — Sort the classes in a unique order specified by a categorical vector, numeric vector, string vector, character array, cell array of character vectors, or logical vector. The array must be a permutation of the
ClassLabels
property of the confusion matrix chart.
Example: sortClasses(cm,'ascending-diagonal')
Example: sortClasses(cm,["owl","cat","toad"])
Version History
Introduced in R2018b
See Also
Functions
Properties
Topics
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)