我有一个数据框,长这样:
df<- data.frame("iteration" = c(1,1,1,1,1,1),
"model" = c("RF","RF","RF","SVM", "SVM","SVM"),
"label" = c(0,0,1,0,0,1), "prediction" = c(0,1,1,0,1,1))
iteration model label prediction
1 1 RF 0 0
2 1 RF 0 1
3 1 RF 1 1
4 1 SVM 0 0
5 1 SVM 0 1
6 1 SVM 1 1
实际上,它有10次迭代
,每个模型都有更多的数据。我想要做的基本上是获取每个模型的准确性。
因此,我想对每个模型组(RF,SVM)应用这个方法:
table(df$label,df$prediction)
0 1
0 2 2
1 0 2
将对角线上的数字相加,然后除以总数:
sum(diag(table(df$label,df$prediction)))/sum(table(df$label,df$prediction))
[1] 0.6666667
这是一个可以使用
tapply
还是 dplyr
更加方便的情况吗?我感到有些迷茫。
sum(label == prediction) / n()
。 - Saul Garcia