本文介绍了Scikit学习,获取每个班级的准确性得分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有内置的方法可以分别获取每个班级的准确性得分?我知道在sklearn中,我们可以使用metric.accuracy_score
来获得整体准确性.有没有一种方法可以细分各个类别的准确性得分?类似于metrics.classification_report
.
Is there a built-in way for getting accuracy scores for each class separatetly? I know in sklearn we can get overall accuracy by using metric.accuracy_score
. Is there a way to get the breakdown of accuracy scores for individual classes? Something similar to metrics.classification_report
.
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]
target_names = ['class 0', 'class 1', 'class 2']
classification_report
没有给出准确性得分:
classification_report
does not give accuracy scores:
print(classification_report(y_true, y_pred, target_names=target_names, digits=4))
Out[9]: precision recall f1-score support
class 0 0.5000 1.0000 0.6667 1
class 1 0.0000 0.0000 0.0000 1
class 2 1.0000 0.6667 0.8000 3
avg / total 0.7000 0.6000 0.6133 5
准确度评分仅给出总体准确度:
Accuracy score gives only the overall accuracy:
accuracy_score(y_true, y_pred)
Out[10]: 0.59999999999999998
推荐答案
您可以使用sklearn的混淆矩阵以获取准确性
You can use sklearn's confusion matrix to get the accuracy
from sklearn.metrics import confusion_matrix
import numpy as np
y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]
target_names = ['class 0', 'class 1', 'class 2']
#Get the confusion matrix
cm = confusion_matrix(y_true, y_pred)
#array([[1, 0, 0],
# [1, 0, 0],
# [0, 1, 2]])
#Now the normalize the diagonal entries
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
#array([[1. , 0. , 0. ],
# [1. , 0. , 0. ],
# [0. , 0.33333333, 0.66666667]])
#The diagonal entries are the accuracies of each class
cm.diagonal()
#array([1. , 0. , 0.66666667])
参考
这篇关于Scikit学习,获取每个班级的准确性得分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!