3

I tried to display my confusion matrix from actual and predicted values while using both functions plotconfusion and Confusionmat. Both give different results. It's really strange for me. It seems Confusionmat is the transpose of plotconfusion. What should I do to plot similar results of Confusionmat on plotconfusion?

plotconfusion:    

59     0     0
0    68      0
0     3     48

And

Confusionmat:

59     0     0
0    68      3
0     0     48
EBH
  • 10,350
  • 3
  • 34
  • 59
Case Msee
  • 405
  • 5
  • 17
  • could be just swaped axis. In one Matrix Prediction label is vertical and True label horizontal. On the other "plot" Prediction label is horizontal and True label vertical. I don't know that beause i don't use matlab for machine learning stuff. But would make sense if you look at your matrices posted. – Florian H Jul 07 '17 at 10:17

1 Answers1

1

You've understood things correctly - the confusion matrix generated by plotconfusion is the transpose of the confusion matrix generated by confusionmat.

This is documented - in the doc for plotconfusion it says

the rows correspond to the predicted class (Output Class), and the columns show the true class (Target Class).

and in the doc for confusionmat it says

C(i,j) is a count of observations known to be in group i but predicted to be in group j

If you want to convert between the two, just transpose them using '.

Why is it like this? Mostly for not very good reasons. plotconfusion is from Neural Network Toolbox, whereas confusionmat is from Statistics Toolbox, and the two toolboxes have different histories, purposes and conventions.

Statistics Toolbox has always been developed directly by MathWorks. By contrast, Neural Network Toolbox was originally developed by external academic authors, and marketed and sold by MathWorks (although recently much development has been brought in-house). Early versions of Neural Network Toolbox were mostly focussed on the application of neural networks to control theory, not to predictive modelling. So the toolboxes had a different history and purpose, and built up a different set of conventions.

It would make sense nowadays to gradually make the toolboxes more consistent and unified in their design, but that hasn't been done yet.

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
  • 4
    Please note that `'` is not the transpose, but the *complex conjugate transpose*. If for some reason complex numbers come out of the calculation, things go wrong using `'` , so either use `transpose(A)` explicitly, or use the *element wise transpose*, `.'` . See e.g. [this question on that](https://stackoverflow.com/q/25150027/5211833) – Adriaan Jul 07 '17 at 11:11
  • Thanks @Adriaan , I'm aware of the distinction between `'` and `.'`, but it seems a bit pedantic to insist on it here, given that we're talking about a confusion matrix, the elements of which are by definition real. It's very common, and perfectly acceptable, to use `'` in that circumstance. – Sam Roberts Jul 07 '17 at 11:30
  • 1
    Even if in this case the numbers are real, spreading the use of `'` for standard is bad in my opinion. I've seen it backfire many times in the past, for different people – Luis Mendo Jul 07 '17 at 14:17
  • @LuisMendo I've seen it backfire as well, for people who are working in domains that involve complex numbers. For people who are working in statistics or data analysis, I've never seen it backfire, because the elements are all going to be real; and if there are complex numbers present, you have a bigger problem than merely getting the conjugate of what you expected. Sure, spread awareness of the distinction, but if my code feels more natural using `'` than `.'`, I maintain that it's perfectly acceptable. Downvote away if you disagree! – Sam Roberts Jul 07 '17 at 14:35
  • 2
    Why did it backfire for people who were working in domains that involved complex numbers? The reason is simply that they got used to the notation `'` while working in a domain that involved real numbers. – Sardar Usama Jul 07 '17 at 22:44