Plot Confusion Matrix for a Trained Classifier

2 views (last 30 days)
I trained a classifier for 7500 instances and 3 classes. Since the confusion matrix tab inside the Classifier App will not let me change font size and title (the most absurd thing ever...) I had to export the classifier as a function and do it manually. So I calculate the validationPredictions as suggested in the generated .m file
partitionedModel = crossval(trainedClassifier.ClassificationSVM, 'KFold', 5);
% Compute validation accuracy
validationAccuracy = 1 - kfoldLoss(partitionedModel, 'LossFun', 'ClassifError');
% Compute validation predictions and scores
[validationPredictions, validationScores] = kfoldPredict(partitionedModel);
This should be trivial at this point, but apparently it is not. When using:
C = confusionmat(response, validationPredictions);
the output C matrix is a 3-by-3 containing the real vs predicted classes. Then running:
plotconfusion(response,validationPredictions)
hangs Matlab and nothing shows up (this should take a split second). What is the issue?

Answers (2)

James Herman
James Herman on 11 Jul 2019
Alessandro - I figured out why this isn't working for me, the data type of the arguments passed to the function (plotconfusion) needs to be "categorical". Even if you're passing a list of integers you need to convert it using the "categorical" function.
  1 Comment
Towfeeq Fairooz
Towfeeq Fairooz on 19 Dec 2021
It has been a long time but I thought this might help someone. So.
You are correct; I experienced the same problem and was only able to resolve it by adding categorical to the arguments of classifier app's (export function)
However, I did not clip any sections of the trainedClassier code it remains well within the code.
inputData =[X,y]; % X = data features, y = target
[myClassifier, myValidationAccuracy] = trainClassifier(inputData);
y_predicted = myClassifier.predictFcn(X_inputData);
plotconfusion(categorical(y_inputData),categorical(y predicted));

Sign in to comment.


Ali Moshrefzadeh
Ali Moshrefzadeh on 26 May 2019
The form of the input matrixes for
plotconfusion(targets,outputs)
is different form the output of
C = confusionmat
You should either use confusionchart(C) to plot the output of confusionmat as a confusion matrix or prepare the proper matrixes for plotconfusion.
  1 Comment
James Herman
James Herman on 10 Jul 2019
You're incorrect. Alessandro Fascetti has used the function precisedly as it is intended. I have the same issue - the function "plotconfusion" doesn't return an error, it simply hangs up during execution.

Sign in to comment.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!