2
$\begingroup$

I have classified the 15 classes dataset using machine learning algorithms. I don't want to put confusion matrix table since it is 15x15 matrix, instead, I want to represent it in a graphical way, which is more pleasant to visualize. Would you kindly recommend any technique? It would be better if it is using ggplot. Thank you so much. Waiting for your reply.

$\endgroup$
  • $\begingroup$ Correspondence analysis is a nice way to visualize relations in a contingency table, of which a confusion matrix is just a case $\endgroup$ – ttnphns Jul 11 '17 at 6:54
  • $\begingroup$ A Heat map would do the job tinyurl.com/yckdyq8f. $\endgroup$ – Anis Nouri Jul 11 '17 at 10:06
2
$\begingroup$
library('reshape2')
library('ggplot2')
mydata <- mtcars[, c(1,3,4,5,6,7)]
cormat <- round(cor(mydata),2)
melted_cormat <- melt(cormat)
ggplot(data = melted_cormat, aes(x=Var1, y=Var2, fill=value)) + geom_tile()

The above code calculates correlation between different features of the cars data. The correlation matrix is similar to confusion matrix both of them are square matrices.

Below is the visualisation of the correlation matrix.

enter image description here

If you have any doubts please comment below.

$\endgroup$
  • $\begingroup$ So your advice is simply to replace cell values (frequencies) by colouration or shade? $\endgroup$ – ttnphns Jul 11 '17 at 6:59
  • $\begingroup$ Thanks for the reply, it's working, but one problem, as I have already mentioned, that I have 15x15 matrix. After reshaping using melt function when I plot it, on the x-axis it is not continuous numbers, I mean, x-axis should vary from number 1,2,3...15. but its varying from 1,10,11,12,13,14,15,2,3,4,5,6,7,8,9. It is not in proper order, I don't know why its happening, would you kindly address it? I hope you understand my problem. Thank you. $\endgroup$ – M Arjun Jul 11 '17 at 10:13
  • $\begingroup$ @MArjun sounds like you have the data in alphabetical order as would happen to string variables. Make sure the data is seen as numerical (a number instead). $\endgroup$ – IWS Jul 11 '17 at 11:38
0
$\begingroup$

To talk about the intuition behind visualizing the confusion matrix:

  1. You essentially have a matrix of real continuous values
  2. For every cell you need to get a sense of more or less

i.e. if your columns refer to predicted class names & row names refer to actual class names, for each cell you want a sense of whether more values were predicted in the class or less.

Considering the above two points, heatmap is the best visualization. (R code example)

Instead of default gradient in the example above, I suggest exploring RColorBrewer palette Spectral to create a slightly more visually descriptive color scheme.

$\endgroup$
0
$\begingroup$

If the number of classes is limited, the chord diagram may be useful.

A port from the d3 library is availaible in R (see the following github repo).

enter image description here

require(chorddiag)

m <- matrix(c(11975,  5871, 8916, 2868,
              1951, 10048, 2060, 6171,
              8010, 16145, 8090, 8045,
              1013,   990,  940, 6907),
            byrow = TRUE,
            nrow = 4, ncol = 4)
classNames <- c("Class A", "Class B", "Class C", "Class D")
dimnames(m) <- list(have = classNames,
                    prefer = classNames)


groupColors <- c("#000000", "#FFDD89", "#957244", "#F26223")
chorddiag(m, groupColors = groupColors, groupnamePadding = 20, showTicks = F )
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.