将confusionMatrix的输出保存为.csv表 [英] Saving output of confusionMatrix as a .csv table
问题描述
我有一个下面的代码产生一个表格的输出
lvs < - c异常)
truth< - factor(rep(lvs,times = c(86,258)),
levels = rev(lvs))
pred < $ bc(
rep(lvs,times = c(54,32)),
rep(lvs,times = c(27,231))),
levels = rev )
xtab< - table(pred,truth)
库(插入符号)
confusionMatrix(xtab)
confusionMatrix ,真相)
confusionMatrix(xtab,prevalence = 0.25)
下面部分输出为 .csv
表
准确性:0.8285
95%CI:(0.7844,0.8668)
无信息速率:0.75
P值[Acc> NIR]:0.0003097
Kappa:0.5336
Mcnemar的测试P值:0.6025370
灵敏度:0.8953
特异性:0.6279
Pos Pred价值:0.8783
负预定值:0.6667
流行率:0.7500
检测率:0.6715
检测率:0.7645
平衡精度:0.7616
尝试将其写为 .csv
表会导致错误消息:
write.csv(confusionMatrix(xtab),file =file.csv)
/ pre>
as.data .frame.default(x [[i]],optional = TRUE,stringsAsFactors = stringsAsFactors):
无法强制类confusionMatrix到数据框架
出于显而易见的原因,手动执行整个工作是不切实际的,并且容易出现人为错误。
有关如何将其导出为
.csv
的任何建议?解决方案确定,所以如果你检查
confusionMatrix(xtab,prevalence = 0.25)
的输出,cm < - confusionMatrix(pred,truth)
str(cm)
列表5
$ positive:chrabnormal
$ table:'table'int [1:2,1:2] 231 27 32 54
..- attr dim)= 2的列表
.. .. $预测:chr [1:2]异常正常
.. .. $参考:chr [1:2]异常 normal
$ overall:Named num [1:7] 0.828 0.534 0.784 0.867 0.75 ...
..- attr(*,names)= chr [1:7] KappaAccuracyLowerAccuracyUpper...
$ byClass:Named num [1:8] 0.895 0.628 0.878 0.667 0.75 ...
..- attr(*,names)= chr [1:8]SensitivitySpecificityPos Pred ValueNeg Pred Value...
$ dots:list()
- attr(*,class)= chr confusionMatrix
从这里开始,选择要从中创建csv的适当对象一个data.frame,每个变量都有一个列。在您的情况下,这将是:tocsv< - data.frame(cbind(t(cm $ overall),t (cm $ byClass)))
/ pre>
#然后可以使用
write.csv(tocsv,file =file.csv)
I have a following code resulting in a table-like output
lvs <- c("normal", "abnormal") truth <- factor(rep(lvs, times = c(86, 258)), levels = rev(lvs)) pred <- factor( c( rep(lvs, times = c(54, 32)), rep(lvs, times = c(27, 231))), levels = rev(lvs)) xtab <- table(pred, truth) library(caret) confusionMatrix(xtab) confusionMatrix(pred, truth) confusionMatrix(xtab, prevalence = 0.25)
I would like to export the below part of the output as a
.csv
tableAccuracy : 0.8285 95% CI : (0.7844, 0.8668) No Information Rate : 0.75 P-Value [Acc > NIR] : 0.0003097 Kappa : 0.5336 Mcnemar's Test P-Value : 0.6025370 Sensitivity : 0.8953 Specificity : 0.6279 Pos Pred Value : 0.8783 Neg Pred Value : 0.6667 Prevalence : 0.7500 Detection Rate : 0.6715 Detection Prevalence : 0.7645 Balanced Accuracy : 0.7616
Attempt to write it as a
.csv
table results in the error message:write.csv(confusionMatrix(xtab),file="file.csv") Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class ""confusionMatrix"" to a data.frame
Doing the whole work manually, for obvious reasons, is impractical and prone to human errors.
Any suggestions on how to export it as a
.csv
?解决方案Ok, so if you inspect the output of
confusionMatrix(xtab, prevalence = 0.25)
, it's a list:cm <- confusionMatrix(pred, truth) str(cm) List of 5 $ positive: chr "abnormal" $ table : 'table' int [1:2, 1:2] 231 27 32 54 ..- attr(*, "dimnames")=List of 2 .. ..$ Prediction: chr [1:2] "abnormal" "normal" .. ..$ Reference : chr [1:2] "abnormal" "normal" $ overall : Named num [1:7] 0.828 0.534 0.784 0.867 0.75 ... ..- attr(*, "names")= chr [1:7] "Accuracy" "Kappa" "AccuracyLower" "AccuracyUpper" ... $ byClass : Named num [1:8] 0.895 0.628 0.878 0.667 0.75 ... ..- attr(*, "names")= chr [1:8] "Sensitivity" "Specificity" "Pos Pred Value" "Neg Pred Value" ... $ dots : list() - attr(*, "class")= chr "confusionMatrix"
From here on you select the appropriate objects that you want to create a csv from and make a data.frame that will have a column for each variable. In your case, this will be:
tocsv <- data.frame(cbind(t(cm$overall),t(cm$byClass))) # You can then use write.csv(tocsv,file="file.csv")
这篇关于将confusionMatrix的输出保存为.csv表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!