sortClasses
Sort classes of confusion matrix chart
Syntax
Description
Examples
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"])
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
Confusion matrix chart, specified as a ConfusionMatrixChart
object. To create a confusion matrix chart, use confusionchart
,
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.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- 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)