问题
I have a question concerning the construction of confusion matrix from the below link: Ranger Predicted Class Probability of each row in a data frame
If I have the following code for example (as explained by the answer in the link):
library(ranger)
library(caret)
idx = sample(nrow(iris),100)
data = iris
data$Species = factor(ifelse(data$Species=="versicolor",1,0))
Train_Set = data[idx,]
Test_Set = data[-idx,]
mdl <- ranger(Species ~ ., ,data=Train_Set,importance="impurity", save.memory = TRUE, probability=TRUE)
probabilities <- as.data.frame(predict(mdl, data = Test_Set,type='response', verbose = TRUE)$predictions)
max.col(probabilities) - 1
Invoking
confusionMatrix(table(Test_Set$Species, max.col(probabilities)-1))
yields:
And, using this
caret::confusionMatrix(table(max.col(probabilities) - 1,Test_Set$Species))
gives
Which is the right way to create confusion matrix, since the values of sensitivity, specificity, ppv, npv differs becuase tp, tn, fp, fn switches?
If I demand the positive class to be 1 rather using
caret::confusionMatrix(table(max.col(probabilities) - 1,Test_Set$Species), positive = '1')
I get
So, the values in the matrices are tp = 13, tn = 36, fp = 0, fn = 1, correct?
I am confused as to how to read the values of the confusion matrix.
回答1:
I have understood the construction of confusion matrices and the role of the entries if the class is changed.
The confusion matrices for the class 0 obtained using
caret::confusionMatrix(table(max.col(probabilities) - 1,Test_Set$Species), positive = '0')
and that of class 1 obtained using
caret::confusionMatrix(table(max.col(probabilities) - 1,Test_Set$Species), positive = '1')
are the same, and
In case of class 0: tp = 36, tn = 13, fp = 1, fn = 0, and in case of class 1: tp = 13, tn = 36, fp = 0, fn = 1 (the roles of tp and tn, and that of fp and fn are switched).
来源:https://stackoverflow.com/questions/61501935/construction-of-confusion-matrix