Welcome to the website, this is a variation of a commonly asked question. You can definitely convert a multi class matrix to a binary conf matrix.
Below is some R code on how you can collapse a confusion matrix to a binary one. It also calculates Cohen's kappa to get the overall 'rater' agreement between the classifeir and the actual class (of cmg
).
cmg <- matrix(c(1639, 116, 49, 35, 138, 0, 0, 236,
150, 274, 27, 21, 28, 0, 0, 73,
22, 24, 58, 9, 94, 0, 0, 30,
33, 27, 31, 21, 146, 0, 0, 49,
14, 9, 5, 1, 49, 0, 0, 22,
1, 0, 1, 1, 7, 0, 0, 6,
11, 0, 0, 1, 14, 0, 0, 21,
201, 11, 8, 5, 49, 0, 0, 253),
ncol=8,dimnames = rep(list(("T1","T2","T3","T4","T5", "T6","T7", "T8")),2))
require(psych)
# Overall agreement
overall_agg <- sum(diag(cmg))/sum(cmg)
# Overall Cohen's Kappa for cmg
unweighted_kappa <- cohen.kappa( cmg, n.obs=sum(cmg) )
# initialise containers
spec_agr_guideline <- list()
collapsed_mat_guideline <- list()
unweighted_kappa_psych <- list()
# loop through all treatments
for (i in seq(1,nrow(cmg)) ) {
# Specific agreements
spec_agr_guideline [i] <- 2*cmg[i,i] / (sum(cmg[i,]) + sum (cmg[,i]) )
# Collapsed positive agreement confusion matrices per treatment
collapsed_mat_guideline[[i]] <- matrix(c(cmg[i,i], sum(cmg[i,])-cmg[i,i],
sum(cmg[,i])-cmg[i,i], sum(cmg)-sum(cmg[i,])-sum(cmg[,i])+cmg[i,i]),
ncol=2)
# Calculate unweighted Cohen's Kappa per collapsed (binary) confusion amtrix
unweighted_kappa_psych[[i]] <- cohen.kappa( collapsed_mat_guideline[[i]], n.obs=sum(collapsed_mat_guideline[[i]]) )
Furthermore, you can do some other cool stuff to assess the performance of a multi-class classifier. Some relevant answers from CrossValidated.com are: link1, link2, link3.
True Positive, True Negative, False Positive, and False Negative are defined for binary classification problems (in which one outcome is defined as "positive" and the other "negative").
So to use this terminology as a measure of success for a larger confusion matrix, you must break the table down into a series of binary classification sub-tables.
For example, we could calculate the TP, TN, FP, and FN for class A. This breaks down as follows
- TP: True A's that are correctly classified as A
- TN: All other classes correctly classified as not A
- FP: Other classes incorrectly classified as A
- FN: A's that were incorrectly classified as not A
This could then be repeated for all classes to get a TP, TN, FP, and FN conditional on each actual class.
Alternatively, you could use a measure calculated on the table as a whole such as overall accuracy. More sophisticated measures can also be calculated to evaluate purity of the classification. This Stack Overflow link provides a good start for investigating some of these methods: how to calculate classification error rate
Best Answer
When I understand your question correctly you are asking which class is the positive one and which is the negative one.
The answer is that this is to a certain extent arbitrary, so you have to decide that considering the problem at hand.
From "Machine Learning with R" by Brett Lantz, 2.nd edition, 2015, p. 318:
This is the reason that you e.g. have to specify the positive class when using generic performance measure functions, like
ConfusionMatrix
in thecaret
package in R.Now another complicating factor is of course your multiclass setting, but this is answered here:
How to compute precision/recall for multiclass-multilabel classification?
In general the most popular approach is to calculate these measures for each class by comparing each class level to the remaining levels (i.e. a "one versus all" approach).