Plot Confusion Matrix for a Trained Classifier

18 次查看(过去 30 天)
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?

回答(2 个)

James Herman
James Herman 2019-7-11
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 个评论
Towfeeq Fairooz
Towfeeq Fairooz 2021-12-19
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));

请先登录,再进行评论。


Ali Moshrefzadeh
Ali Moshrefzadeh 2019-5-26
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 个评论
James Herman
James Herman 2019-7-10
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.

请先登录,再进行评论。

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by