Error using confusionchart (line 68) Order must be an exact permutation of the class labels.
5 views (last 30 days)
Show older comments
Ernest Modise - Kgamane
on 16 Jun 2024
Commented: Abdulqader faris
on 2 Jul 2024
Please help with this error - what could be the cause?
Order must be an exact permutation of the class labels.
2 Comments
Abdulqader faris
on 2 Jul 2024
can u help me my error in the code is :
Error using confusionchart (line 68)
Vectors of true and predicted labels must have the same number of observations.
Error in untitled5 (line 88)
confusionchart(TTest,YTest);
Accepted Answer
Shivani
on 18 Jun 2024
Hello @Ernest Modise - Kgamane
The error you're encountering with confusionchart in MATLAB indicates that the order of the classes specified in the confusion matrix does not match the order of the class labels provided. To resolve this error, you need to ensure that the order of the classes in the confusion matrix matches the order of the class labels.
While it is difficult to determine the exact reason for this error without the data and code, some of the reasons this can happen include mismatch in class labels, duplicates or missing labels, incorrect data type, and unrecognized labels.
The following workarounds may help resolve the error you are encountering:
- Check the class labels in your dataset and ensure that the sorting array is an exact permutation of these labels. No label should be missing, and there should be no additional labels that are not present in your data. You can use the unique function to find all unique labels in your true labels and predicted labels arrays. You can use the following link for implementation details on using the unique function: https://www.mathworks.com/help/matlab/ref/double.unique.html
- Ensure there are no typos in your class labels
- Make sure the sorting array and the class labels in your dataset are of the same data type
- The sorting array should include each class label from your dataset exactly once, in the order you wish to have them displayed in the confusion matrix.
Since I don't have access to the dataset you're using, I'll include a simple example with some sample data to illustrate how this issue can be resolved:
trueLabels = [1; 2; 3; 1; 2; 3; 1; 2; 3];
predictedLabels = [1; 2; 3; 2; 1; 3; 3; 1; 2];
%additional class label '4' will cause an error since it is not present in trueLabels or predictedLabels
%classLabels = [1; 2; 3; 4]; %uncommenting this line will replicate the error
% Define the class labels
classLabels = [1; 2; 3];
% Create the confusion matrix and the chart
confusionMat = confusionmat(trueLabels, predictedLabels);
confusionchart(confusionMat, classLabels);
Additionally, you can refer to the following MATLAB documentation link for more details on implementation: https://www.mathworks.com/help/stats/confusionchart.html
0 Comments
More Answers (0)
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!