# How to make a confusion matrix from comparing prediction results of two algorithms?

I applied two unsupervised algorithms to the same data, and would like to make a confusion matrix out of results, how should I achieve it in R?

An example with R codes like following:

xx.1 <- c(41, 0, 4, 0, 0, 0, 0, 0, 0, 7, 0, 11, 8, 0, 0, 0, 0, 0, 3, 0, 0, 1, 1, 0, 4)
xx.2 <- matrix(xx.1, nrow = 5)
rownames(xx.2) <- paste("Algo1", 1:5, sep = "_")
colnames(xx.2) <- paste("Algo2", 1:5, sep = "_")
xx.2


xx.2 is the predicting results of two algorithms, the numbers show how many observation are classified as Algo1_X and Algo2_X:

       Algo2_1 Algo2_2 Algo2_3 Algo2_4 Algo2_5
Algo1_1      41       0       0       0       0
Algo1_2       0       0      11       0       1
Algo1_3       4       0       8       0       1
Algo1_4       0       0       0       3       0
Algo1_5       0       7       0       0       4


The problem is, how should I rearrange the matrix to get a confusion matrix, by using the results of Algo1 as reference?

The relationship of clusters between Algo1 and Algo2 is inferred from the matrix above, i.e., 41 is the largest number between Algo2_1 and Algo1_1, so Algo2_1 and Algo1_1 are paired.

You could use pd.crosstab as a cross-tabulation function to make a confusion matrix from your two comparison sets.

Please see this for an example

• Suggests using pandas, the question is about R, Commented Nov 29, 2017 at 19:08

You can use confusionMatrix from the caret package to create a confusion matrix (example).

The nature of a confusion matrix is such that you use it to compare predicted class versus actual class for one classifier though. You could create two different confusion matrices, and compare results that way.