多类别模型的准确性,精确度和召回率 [英] Accuracy, precision, and recall for multi-class model
问题描述
如何从混淆矩阵中为每个类别计算准确性,精确度和召回?我正在使用嵌入式数据集虹膜;混淆矩阵如下:
How do I calculate accuracy, precision and recall for each class from a confusion matrix? I am using the embedded dataset iris; the confusion matrix is as below:
prediction setosa versicolor virginica
setosa 29 0 0
versicolor 0 20 2
virginica 0 3 21
我正在使用75个条目作为训练集,而其他用于测试:
I am using 75 entries as the training set and other for testing:
iris.train <- c(sample(1:150, 75)) # have selected 75 randomly
推荐答案
在这个答案中, mat
是您描述的混淆矩阵.
Throughout this answer, mat
is the confusion matrix that you describe.
您可以使用以下方法计算和存储准确性:
You can calculate and store accuracy with:
(accuracy <- sum(diag(mat)) / sum(mat))
# [1] 0.9333333
每个类的精度(假设预测在行上,真实结果在列上)可以通过以下方式计算:
Precision for each class (assuming the predictions are on the rows and the true outcomes are on the columns) can be computed with:
(precision <- diag(mat) / rowSums(mat))
# setosa versicolor virginica
# 1.0000000 0.9090909 0.8750000
如果要获取特定类的精度,可以执行以下操作:
If you wanted to grab the precision for a particular class, you could do:
(precision.versicolor <- precision["versicolor"])
# versicolor
# 0.9090909
对于每个班级的回忆(同样假设预测在行上,真实结果在列上)可以使用以下公式计算:
Recall for each class (again assuming the predictions are on the rows and the true outcomes are on the columns) can be calculated with:
recall <- (diag(mat) / colSums(mat))
# setosa versicolor virginica
# 1.0000000 0.8695652 0.9130435
如果您想召回特定班级,可以执行以下操作:
If you wanted recall for a particular class, you could do something like:
(recall.virginica <- recall["virginica"])
# virginica
# 0.9130435
如果相反,您将真实结果作为行,将预测结果作为列,那么您将翻转精度并调用定义.
If instead you had the true outcomes as the rows and the predicted outcomes as the columns, then you would flip the precision and recall definitions.
数据:
(mat = as.matrix(read.table(text=" setosa versicolor virginica
setosa 29 0 0
versicolor 0 20 2
virginica 0 3 21", header=T)))
# setosa versicolor virginica
# setosa 29 0 0
# versicolor 0 20 2
# virginica 0 3 21
这篇关于多类别模型的准确性,精确度和召回率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!