Plot Confusion Matrix for a Trained Classifier

22 visualizaciones (últimos 30 días)
Alessandro Fascetti
Alessandro Fascetti el 5 de Oct. de 2018
Comentada: Towfeeq Fairooz el 19 de Dic. de 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?

Respuestas (2)

James Herman
James Herman el 11 de Jul. de 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 comentario
Towfeeq Fairooz
Towfeeq Fairooz el 19 de Dic. de 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));

Iniciar sesión para comentar.


Ali Moshrefzadeh
Ali Moshrefzadeh el 26 de Mayo de 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 comentario
James Herman
James Herman el 10 de Jul. de 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.

Iniciar sesión para comentar.

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by