混淆矩阵的构造 [英] Construction of confusion matrix
问题描述
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
调用
confusionMatrix(table(Test_Set$Species, max.col(probabilities)-1))
收益:
然后,使用此
caret::confusionMatrix(table(max.col(probabilities) - 1,Test_Set$Species))
提供
哪种方法是创建混淆矩阵的正确方法,因为灵敏度,特异性,ppv,npv的值因tp,tn,fp,fn开关的不同而不同?
Which is the right way to create confusion matrix, since the values of sensitivity, specificity, ppv, npv differs becuase tp, tn, fp, fn switches?
如果我要求将正类设为1而不是使用
If I demand the positive class to be 1 rather using
caret::confusionMatrix(table(max.col(probabilities) - 1,Test_Set$Species), positive = '1')
我得到
那么,矩阵中的值是tp = 13,tn = 36,fp = 0,fn = 1,对吗?
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.
推荐答案
我了解混淆矩阵的构造,以及如果更改了类,则条目的作用.
I have understood the construction of confusion matrices and the role of the entries if the class is changed.
使用以下方法获得的0类的混淆矩阵
The confusion matrices for the class 0 obtained using
caret::confusionMatrix(table(max.col(probabilities) - 1,Test_Set$Species), positive = '0')
以及使用
caret::confusionMatrix(table(max.col(probabilities) - 1,Test_Set$Species), positive = '1')
和
对于第0类,tp = 36,tn = 13,fp = 1,fn = 0,对于第1类:tp = 13,tn = 36,fp = 0,fn = 1(角色tp和tn的值,以及fp和fn的值被切换).
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).
这篇关于混淆矩阵的构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!