Construction of confusion matrix

此生再无相见时 提交于 2020-05-15 21:20:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!