Plot confusion doesn't work

41 views (last 30 days)
Olga Zhukova
Olga Zhukova on 17 Dec 2013
Answered: Hamid Salimi on 9 Jun 2021
Hello,
I have the problem that when I try use plotconfusion, this function doesn't work.
I have dataset with 15 classes and I try to predict the target value using knn-classification. I've divided datasets to training and test datasets (75:25 accordinaly). My dataset has 300 instances and 90 attributes.
The problem is that when I try to call this plotconfusion function I just see that this doesn't work (it somehow just go to a infinite cycle or something like this, the process doesn't terminate). Could you tell me what's the problem or do I use it wrong?
Here the part of my code: knn = ClassificationKNN.fit(XtrainNN,YtrainNN,'NumNeighbors',5); Y_knn = knn.predict(XtestNN); loss(knn, XtestNN, YtestNN) plotconfusion(Y_knn,YtestNN)
  2 Comments
Baran Yildiz
Baran Yildiz on 26 Sep 2017
I am also having the same problem. Plot confusion doesn't seem to work even for the sample problem/dataset given in the reference link below:
https://au.mathworks.com/help/nnet/ref/plotconfusion.html#inputarg_targets

Sign in to comment.

Answers (3)

Nathan DeJong
Nathan DeJong on 27 Sep 2017
Try transposing the inputs so that they are row vectors rather than column vectors. It worked for me. Seems to be a strange bug in plotconfusion().
  1 Comment
Pedro Borges
Pedro Borges on 17 Oct 2018
transposing the inputs worked for me, too! thanks!

Sign in to comment.


Hamid Salimi
Hamid Salimi on 9 Jun 2021
I write it for anyone that may have the same problem, I solved it by converting my actual and predicted results to categorical data! your actual and predicted should be n * 1, and then use it:
plotconfusion(categorical(actual),categorical(predicted));

Ilya
Ilya on 17 Dec 2013
I never used plotconfusion, but you can get what you want using functions confusionmat and imagesc. For example,
knn = ClassificationKNN.fit(XtrainNN,YtrainNN,'NumNeighbors',5);
Y_knn = knn.predict(XtestNN);
cm = confusionmat(YtestNN,Y_knn);
imagesc(cm);
colorbar;

Community Treasure Hunt

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

Start Hunting!