For subsequent discussion, I am using the keras package in R.
Given a confusion matrix created as follows:
# Get confusion matrix for predictions
classes <- model %>% predict_classes(test, batch_size=128)
ct <- table(test.target, classes)
cm <- as.matrix(ct)
For which ct
gives the following confusion matrix:
classes
test.target 0 1 2
0 805 192 0
1 74 862 0
2 2 0 477
How can I calculate the True Positive (TP), False Positive (FP), True Negative (TN) and False Negative (FN) values?
For clarification, I calculate the True Positive (TP) value by getting the diagonal of the matrix:
tp <- diag(cm)
However, my attempt of calculating the FP value gives me negative numbers (which I guess cant be right, correct?):
# Get false positive rates (FP)
fp <- c()
for(i in seq_len(ncol(ct))) {
fp <- append(fp, sum(cm[,i])-cm[i,i])
}
EDIT: The dput(cm)
is as follows:
structure(c(805L, 74L, 2L, 192L, 862L, 0L, 0L, 0L, 477L), .Dim = c(3L,
3L), .Dimnames = list(test.target = c("0", "1", "2"), classes = c("0",
"1", "2")), class = "table")