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.
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.
If you have any doubts please comment below.
To talk about the intuition behind visualizing the confusion matrix:
- You essentially have a matrix of real continuous values
- 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
Spectral to create a slightly more visually descriptive color scheme.
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).
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 )