Confusion chart displaying wrong number of class labels

4 views (last 30 days)
Hi,
I'm classifying accelerometer position data into 2 classes: movement or stable using a binary decision tree model. I've applied the model to my test data and I'm trying to plot the confusion chart. However, the confusion chart appears to have 4 class labels (1, 2, movement, stable) when the data only has two classes (movement or stable). I'm not sure why this is happening.
I have attached my data (actTest (known classes) and TestPredModel (predicted classes)), as well as an image of the confusion matrix I get when I run the following code:
confusionchart(actTest,TestPredModel,'Normalization','row-normalized')
Any help will be much appreciated!
Thanks!
  2 Comments
Impala
Impala on 17 Jan 2023
It appears that if I accidentally type something into one of the cells of my categorical variables (actTest, TestPredModel), which is not one of my classes and then correct it later, whatever I typed appears on the confusion chart.
e.g. if I type 'A' in actTest(1,1) and then correct it back to its original values ('Stable') and run the confusionchart funtion above, A appears as a class even though I corrected the value. Please see attached image.
Is this a bug?
Impala
Impala on 17 Jan 2023
Sorry, another quick update - when I change the data type to cell arrays, the confusion chart displays the correct number of classes (see attached). I have used the following syntax:
actTest2 = cellstr(actTest);
TestPredModel2 = cellstr(TestPredModel)
confusionchart(actTest2,TestPredModel2,'Normalization','row-normalized')
Why doesn't the confusion chart work well with categorical arrays?

Sign in to comment.

Accepted Answer

Aman
Aman on 16 Mar 2023
Edited: Aman on 16 Mar 2023
Hi,
I understand that you have made model for classification and to evaluate the model performance you are using confusion matrix, but there are incorrect number of classes in the confusion chart.
When change the categorical array via cell update or manual update through code, a new category is created. If you later modify the same cell, the categorical array will be updated but the newly created category will still be there with zero instances. You can use the “removecats” method to remove the unused categories before plotting the confusion chart. Please refer the below code for your reference.
summary(actTest);
actTest = removecats(actTest);
summary(actTest);
confusionchart(actTest,TestPredModel,'Normalization','row-normalized');
Hope it helps!

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!