为什么某些准确性度量未显示在插入符号中(F1、召回率和精度)
Why some accuracy measures aren't showing in caret ( F1 , Recall and precision )
下午好,
假设我们有以下内容:
pred=c(2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1)
obs=structure(c(2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("no-recurrence-events",
"recurrence-events"), class = "factor")
以及以下函数:
library(plyr)
library(caret)
metrics<-function(observed1,predicted1){
#predicted1=mapvalues(predicted1, from = unique(predicted1), to = unique(observed1))
observed1=mapvalues(observed1, from = unique(observed1), to = unique(predicted1) )
#xtab=table(observed1, predicted1)
xtab=table(predicted1,observed1)
#result<- confusionMatrix(as.factor(predicted1), as.factor(observed1))
result<- confusionMatrix(as.factor(predicted1),as.factor(observed1))
print(recall(as.factor(predicted1), as.factor(observed1))) # i can print recall 0.9285714
return(list(xtab,result))
}
在 运行 之后,F1、召回率和精度等一些指标没有显示:
metrics(obs,pred)
[[1]]
observed1
predicted1 1 2
1 13 4
2 1 2
[[2]]
Confusion Matrix and Statistics
Reference
Prediction 1 2
1 13 4
2 1 2
Accuracy : 0.75
95% CI : (0.509, 0.9134)
No Information Rate : 0.7
P-Value [Acc > NIR] : 0.4164
Kappa : 0.3056
Mcnemar's Test P-Value : 0.3711
Sensitivity : 0.9286
Specificity : 0.3333
Pos Pred Value : 0.7647
Neg Pred Value : 0.6667
Prevalence : 0.7000
Detection Rate : 0.6500
Detection Prevalence : 0.8500
Balanced Accuracy : 0.6310
'Positive' Class : 1
主要是一些准确度指标没有显示,这看起来很奇怪:F1、召回率、精度!
我注意到另一个问题,例如,如果我们颠倒顺序:
metrics(pred,obs)
Error in confusionMatrix.default(as.factor(predicted1), as.factor(observed1)) :
The data must contain some levels that overlap the reference.
Calls: metrics -> confusionMatrix -> confusionMatrix.default
Execution halted
我希望我的问题很清楚。
感谢您的帮助!
我找到了解决办法。 confusionMatrix()
有一个名为 mode='everything'
的选项,它输出所有已实施的措施:
pred=c(2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1)
obs=structure(c(2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("no-recurrence-events",
"recurrence-events"), class = "factor")
library(plyr)
library(caret)
metrics<-function(observed1,predicted1){
#predicted1=mapvalues(predicted1, from = unique(predicted1), to = unique(observed1))
observed1=mapvalues(observed1, from = unique(observed1), to = unique(predicted1) )
#xtab=table(observed1, predicted1)
xtab=table(predicted1,observed1)
#result<- confusionMatrix(as.factor(predicted1), as.factor(observed1))
result<- confusionMatrix(as.factor(predicted1),as.factor(observed1),mode='everything')
return(list(xtab,result))
}
metrics(obs,pred)
这给出了预期的输出:
Loading required package: lattice
Loading required package: ggplot2
[[1]]
observed1
predicted1 1 2
1 13 4
2 1 2
[[2]]
Confusion Matrix and Statistics
Reference
Prediction 1 2
1 13 4
2 1 2
Accuracy : 0.75
95% CI : (0.509, 0.9134)
No Information Rate : 0.7
P-Value [Acc > NIR] : 0.4164
Kappa : 0.3056
Mcnemar's Test P-Value : 0.3711
Sensitivity : 0.9286
Specificity : 0.3333
Pos Pred Value : 0.7647
Neg Pred Value : 0.6667
Precision : 0.7647
Recall : 0.9286
F1 : 0.8387
Prevalence : 0.7000
Detection Rate : 0.6500
Detection Prevalence : 0.8500
Balanced Accuracy : 0.6310
'Positive' Class : 1
下午好,
假设我们有以下内容:
pred=c(2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1)
obs=structure(c(2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("no-recurrence-events",
"recurrence-events"), class = "factor")
以及以下函数:
library(plyr)
library(caret)
metrics<-function(observed1,predicted1){
#predicted1=mapvalues(predicted1, from = unique(predicted1), to = unique(observed1))
observed1=mapvalues(observed1, from = unique(observed1), to = unique(predicted1) )
#xtab=table(observed1, predicted1)
xtab=table(predicted1,observed1)
#result<- confusionMatrix(as.factor(predicted1), as.factor(observed1))
result<- confusionMatrix(as.factor(predicted1),as.factor(observed1))
print(recall(as.factor(predicted1), as.factor(observed1))) # i can print recall 0.9285714
return(list(xtab,result))
}
在 运行 之后,F1、召回率和精度等一些指标没有显示:
metrics(obs,pred)
[[1]]
observed1
predicted1 1 2
1 13 4
2 1 2
[[2]]
Confusion Matrix and Statistics
Reference
Prediction 1 2
1 13 4
2 1 2
Accuracy : 0.75
95% CI : (0.509, 0.9134)
No Information Rate : 0.7
P-Value [Acc > NIR] : 0.4164
Kappa : 0.3056
Mcnemar's Test P-Value : 0.3711
Sensitivity : 0.9286
Specificity : 0.3333
Pos Pred Value : 0.7647
Neg Pred Value : 0.6667
Prevalence : 0.7000
Detection Rate : 0.6500
Detection Prevalence : 0.8500
Balanced Accuracy : 0.6310
'Positive' Class : 1
主要是一些准确度指标没有显示,这看起来很奇怪:F1、召回率、精度!
我注意到另一个问题,例如,如果我们颠倒顺序:
metrics(pred,obs)
Error in confusionMatrix.default(as.factor(predicted1), as.factor(observed1)) :
The data must contain some levels that overlap the reference.
Calls: metrics -> confusionMatrix -> confusionMatrix.default
Execution halted
我希望我的问题很清楚。
感谢您的帮助!
我找到了解决办法。 confusionMatrix()
有一个名为 mode='everything'
的选项,它输出所有已实施的措施:
pred=c(2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1)
obs=structure(c(2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("no-recurrence-events",
"recurrence-events"), class = "factor")
library(plyr)
library(caret)
metrics<-function(observed1,predicted1){
#predicted1=mapvalues(predicted1, from = unique(predicted1), to = unique(observed1))
observed1=mapvalues(observed1, from = unique(observed1), to = unique(predicted1) )
#xtab=table(observed1, predicted1)
xtab=table(predicted1,observed1)
#result<- confusionMatrix(as.factor(predicted1), as.factor(observed1))
result<- confusionMatrix(as.factor(predicted1),as.factor(observed1),mode='everything')
return(list(xtab,result))
}
metrics(obs,pred)
这给出了预期的输出:
Loading required package: lattice
Loading required package: ggplot2
[[1]]
observed1
predicted1 1 2
1 13 4
2 1 2
[[2]]
Confusion Matrix and Statistics
Reference
Prediction 1 2
1 13 4
2 1 2
Accuracy : 0.75
95% CI : (0.509, 0.9134)
No Information Rate : 0.7
P-Value [Acc > NIR] : 0.4164
Kappa : 0.3056
Mcnemar's Test P-Value : 0.3711
Sensitivity : 0.9286
Specificity : 0.3333
Pos Pred Value : 0.7647
Neg Pred Value : 0.6667
Precision : 0.7647
Recall : 0.9286
F1 : 0.8387
Prevalence : 0.7000
Detection Rate : 0.6500
Detection Prevalence : 0.8500
Balanced Accuracy : 0.6310
'Positive' Class : 1