sortClasses
Sort classes of confusion matrix chart
Syntax
Description
Examples
Sort Classes by Precision or Recall
Create a confusion matrix chart and sort the classes of the chart according to the class-wise true positive rate (recall) or the class-wise positive predictive value (precision).
Load and inspect thearrhythmia
data set.
loadarrhythmiaisLabels = unique(Y); nLabels = numel(isLabels)
nLabels = 13
tabulate(categorical(Y))
Value Count Percent 1 245 54.20% 2 44 9.73% 3 15 3.32% 4 15 3.32% 5 13 2.88% 6 25 5.53% 7 3 0.66% 8 2 0.44% 9 9 1.99% 10 50 11.06% 14 4 0.88% 15 5 1.11% 16 22 4.87%
The data contains 16 distinct labels that describe various degrees of arrhythmia, but the response (Y
) includes only 13 distinct labels.
Train a classification tree and predict the resubstitution response of the tree.
Mdl = fitctree(X,Y); predictedY = resubPredict(Mdl);
Create a confusion matrix chart from the true labelsY
and the predicted labelspredictedY
. Specify'RowSummary'
as'row-normalized'
to display the true positive rates and false positive rates in the row summary. Also, specify'ColumnSummary'
as'column-normalized'
to display the positive predictive values and false discovery rates in the column summary.
fig = figure; cm = confusionchart(Y,predictedY,'RowSummary','row-normalized','ColumnSummary','column-normalized');
Resize the container of the confusion chart so percentages appear in the row summary.
fig_Position = fig.Position; fig_Position(3) = fig_Position(3)*1.5; fig.Position = fig_Position;
To sort the confusion matrix according to the true positive rate, normalize the cell values across each row by setting theNormalization
property to'row-normalized'
and then usesortClasses
. After sorting, reset theNormalization
property back to'absolute'
to display the total number of observations in each cell.
cm.Normalization ='row-normalized'; sortClasses(cm,'descending-diagonal') cm.Normalization ='absolute';
To sort the confusion matrix according to the positive predictive value, normalize the cell values across each column by setting theNormalization
property to'column-normalized'
and then usesortClasses
. After sorting, reset theNormalization
property back to'absolute'
to display the total number of observations in each cell.
cm.Normalization ='column-normalized'; sortClasses(cm,'descending-diagonal') cm.Normalization ='absolute';
Sort Classes to Cluster Similar Classes
Create a confusion matrix chart by using theconfusionchart
function, and sort the classes to cluster similar classes by using the'cluster'
option of thesortClasses
函数。This example also shows how to cluster by using thepdist
,linkage
, andoptimalleaforder
functions.
Generate a sample data set that contains eight distinct classes.
rng('default')% For reproducibilitytrueLabels = randi(8,1000,1); predictedLabels = trueLabels;
Insert confusion among classes {1,4,7}, {2,8}, and {5,6} for the first 200 samples.
rename = [4 8 3 7 6 5 1 2]; predictedLabels(1:100) = rename(predictedLabels(1:100)); rename = [7 8 3 1 6 5 4 2]; predictedLabels(101:200) = rename(predictedLabels(101:200));
Create a confusion matrix chart from the true labelstrueLabels
and the predicted labelspredictedLabels
.
figure cm1 = confusionchart(trueLabels,predictedLabels);
集群使用'cluster'
Sort the classes to cluster similar classes by using the'cluster'
option.
sortClasses(cm1,'cluster')
集群使用pdist
,linkage
, andoptimalleaforder
Instead of using the'cluster'
option, you can use thepdist
,linkage
, andoptimalleaforder
functions to cluster confusion matrix values. You can customize clustering by using the options of these functions. For details, see the corresponding function reference pages.
Suppose you have a confusion matrix and class labels.
m = confusionmat(trueLabels,predictedLabels); labels = [1 2 3 4 5 6 7 8];
Compute the clustered matrix and find the corresponding class labels by usingpdist
,linkage
, andoptimalleaforder
. Thepdist
function computes the Euclidean distanceD
between pairs of the confusion matrix values. Theoptimalleaforder
function returns an optimal leaf ordering for the hierarchical binary cluster treelinkage(D)
using the distanceD
.
D = pdist(m); idx = optimalleaforder(linkage(D),D); clusteredM = m(idx,idx); clusteredLabels = labels(idx);
Create a confusion matrix chart using the clustered matrix and the corresponding class labels. Then, sort the classes using the class labels.
cm2 = confusionchart(clusteredM,clusteredLabels); sortClasses(cm2,clusteredLabels)
The sorted confusion matrix chartcm2
, which you created by usingpdist
,linkage
, andoptimalleaforder
, is identical to the sorted confusion matrix chartcm1
, which you created by using the'cluster'
option.
Sort Classes in Fixed Order
Create a confusion matrix chart and sort the classes of the chart in a fixed order.
Load Fisher's iris data set.
loadfisheririsX = meas([51:150,1:50],:); Y = species([51:150,1:50],:);
X
is a numeric matrix that contains four petal measurements for 150 irises.Y
is a cell array of character vectors that contains the corresponding iris species.
Train ak-nearest neighbor (KNN) classifier, where the number of nearest neighbors in the predictors (k) is 5. A good practice is to standardize numeric predictor data.
Mdl = fitcknn(X,Y,'NumNeighbors',5,'Standardize',1);
Predict the labels of the training data.
predictedY = resubPredict(Mdl);
Create a confusion matrix chart from the true labelsY
and the predicted labelspredictedY
.
cm = confusionchart(Y,predictedY);
By default,confusionchart
sorts the classes into their natural order as defined bysort
. In this example, the class labels are character vectors, soconfusionchart
sorts the classes alphabetically. Reorder the classes of the confusion matrix chart in a fixed order.
sortClasses(cm,["versicolor","setosa","virginica"])
Input Arguments
cm
—Confusion matrix chart
ConfusionMatrixChart
object
Confusion matrix chart, specified as aConfusionMatrixChart
object. To create a confusion matrix chart, useconfusionchart
,
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
函数。例如,如果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'
— Sort the classes to cluster similar classes. You can customize clustering by using thepdist
,linkage
, andoptimalleaforder
functions. For details, seeSort Classes to Cluster Similar Classes.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"])
See Also
Functions
Properties
Open Example
A modified version of this example exists on your system. Do you want to open this version instead?
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)