Confusion chart displaying wrong number of class labels

17 views (last 30 days)
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:
Any help will be much appreciated!
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 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)
Why doesn't the confusion chart work well with categorical arrays?

Sign in to comment.

Accepted Answer

Aman on 16 Mar 2023
Edited: Aman on 16 Mar 2023
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.
actTest = removecats(actTest);
Hope it helps!

More Answers (0)




Community Treasure Hunt

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

Start Hunting!