Effacer les filtres
Effacer les filtres

Plot Confusion Matrix for a Trained Classifier

4 vues (au cours des 30 derniers jours)
Alessandro Fascetti
Alessandro Fascetti le 5 Oct 2018
Commenté : Towfeeq Fairooz le 19 Déc 2021
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?

Réponses (2)

James Herman
James Herman le 11 Juil 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 commentaire
Towfeeq Fairooz
Towfeeq Fairooz le 19 Déc 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));

Connectez-vous pour commenter.


Ali Moshrefzadeh
Ali Moshrefzadeh le 26 Mai 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 commentaire
James Herman
James Herman le 10 Juil 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.

Connectez-vous pour commenter.

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by