承接上文《混淆矩阵》,本文通过混淆矩阵获取几个常见的评估指标准确率(Accuracy)、精确度(Precision、召回率(Recall)、F1(F-score)。使用sklearn、tensorflow和手搓混淆矩阵这3种方式进行指标的计算 🦆🦆🦆
python
import sklearn
import numpy as np
import seaborn as sns
import tensorflow as tf
from matplotlib import pyplot as plt
Accuracy 准确率
sklearn.metrics.accuracy_score 计算
python
# 设置预测结果
pred = [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5]
# 设置正确标签
true = [0, 1, 2, 3, 1, 5, 0, 1, 2, 3, 1, 5, 0, 1, 2, 3, 4, 5]
accuracy = sklearn.metrics.accuracy_score(y_true=true, y_pred=pred)
print(accuracy)
==============================
输出:
0.8888888888888888
tf.keras.metrics.Accuracy 计算
python
accuracy = tf.keras.metrics.Accuracy()
accuracy.update_state(y_true=true, y_pred=pred)
print(accuracy.result().numpy())
==============================
输出:
0.8888889
Precision 精准度
sklearn.metrics.precision_score 计算
python
precision = sklearn.metrics.precision_score(y_true=true, y_pred=pred, average='macro')
print(precision)
==============================
输出:
0.8888888888888888
tf.keras.metrics.Precision 计算
python
precision = tf.keras.metrics.Precision()
precision.update_state(y_true=tf.one_hot(true,6), y_pred=tf.one_hot(pred,6))
print(precision.result().numpy())
==============================
输出:
0.8888889
Recall 召回率
sklearn.metrics.recall_score 计算
python
recall = sklearn.metrics.recall_score(y_true=true, y_pred=pred, average='micro')
print(recall)
==============================
输出:
0.8888888888888888
tf.keras.metrics.Recall 计算
python
recall = tf.keras.metrics.Recall()
recall.update_state(y_true=tf.one_hot(true,6), y_pred=tf.one_hot(pred,6))
print(recall.result().numpy())
==============================
输出:
0.8888889
F1 F-score
sklearn.metrics.f1_score
python
f1 = sklearn.metrics.f1_score(y_true=true, y_pred=pred, average='macro')
print(f1)
==============================
输出:
0.875
tf.keras.metrics.F1Score
python
f1 = tf.keras.metrics.F1Score(average='macro')
f1.update_state(y_true=tf.one_hot(true,6), y_pred=tf.one_hot(pred,6))
print(f1.result().numpy())
==============================
输出:
0.875
手搓混淆矩阵
对于计算得到的混淆矩阵,手搓计算准确率(Accuracy)、精确度(Precision、召回率(Recall)、F1(F-score)
别问为啥手搓的指标和sklearn、tensorflow计算的有出入,笔者也不知道呀😅😅😅
构造混淆矩阵
先得出混淆矩阵,对各个类别计算TP、TN、FP、FN,进一步的去计算这些杂七杂八的指标
构造混淆矩阵这里就看懵的,先去look look《混淆矩阵》吧😁
python
cm = sklearn.metrics.confusion_matrix(y_true=true, y_pred=pred)
print(cm)
# 计算混淆矩阵的总和
total = np.sum(cm)
# 计算那条深色斜线的总和
line = np.sum([cm[i, i] for i in range(len(cm))])
# 储存每个类别的 TP TF NP NF
classes_list = []
for i in range(len(cm)):
TP = cm[i, i]
TN = line - TP
FP = sum(cm[:, i]) - TP
FN = total - TP - TN - FP
classes_list.append({i: {'tp': TP, 'tn': TN, 'fp': FP, 'fn': FN}})
classes_list
==============================
输出:
[[3 0 0 0 0 0]
[0 3 0 0 2 0]
[0 0 3 0 0 0]
[0 0 0 3 0 0]
[0 0 0 0 1 0]
[0 0 0 0 0 3]]
[{0: {'tp': 3, 'tn': 13, 'fp': 0, 'fn': 2}},
{1: {'tp': 3, 'tn': 13, 'fp': 0, 'fn': 2}},
{2: {'tp': 3, 'tn': 13, 'fp': 0, 'fn': 2}},
{3: {'tp': 3, 'tn': 13, 'fp': 0, 'fn': 2}},
{4: {'tp': 1, 'tn': 15, 'fp': 2, 'fn': 0}},
{5: {'tp': 3, 'tn': 13, 'fp': 0, 'fn': 2}}]
手搓 Accuracy
<math xmlns="http://www.w3.org/1998/Math/MathML"> A c c u r a c y = T P + T N T P + T N + F P + F N Accuracy = \frac{TP+TN}{TP+TN+FP+FN} </math>Accuracy=TP+TN+FP+FNTP+TN
方式1
python
mean = 0
for element in classes_list:
# 获取各个类别的 TP TN FP FN
tp, tn, fp, fn = list(element.values())[0].values()
# 计算 Accuracy
print(f'类别{list(element.keys())[0]} Accuracy={(tp+tn)/(tp+tn+fp+fn)}')
mean+=(tp+tn)/(tp+tn+fp+fn)
print(f'\n平均 Accuracy={mean/6}')
==============================
输出:
类别0 Accuracy=0.8888888888888888
类别1 Accuracy=0.8888888888888888
类别2 Accuracy=0.8888888888888888
类别3 Accuracy=0.8888888888888888
类别4 Accuracy=0.8888888888888888
类别5 Accuracy=0.8888888888888888
平均 Accuracy=0.888888888888889
方式2
python
total = len(pred)
correct = tf.math.count_nonzero(tf.equal(pred, true)).numpy()
accuracy = correct/total
print(accuracy)
==============================
输出:
0.8888888888888888
手搓 Precision
<math xmlns="http://www.w3.org/1998/Math/MathML"> P r e c i s i o n = T P T P + F P Precision= \frac{TP}{TP+FP} </math>Precision=TP+FPTP
python
mean = 0
for element in classes_list:
# 获取各个类别的 TP TN FP FN
tp, tn, fp, fn = list(element.values())[0].values()
# 计算 Precision
print(f'类别{list(element.keys())[0]} Precision={(tp)/(tp+fp)}')
mean += (tp)/(tp+fp)
print(f'平均 Precision={mean/6}')
==============================
输出:
类别0 Precision=1.0
类别1 Precision=1.0
类别2 Precision=1.0
类别3 Precision=1.0
类别4 Precision=0.3333333333333333
类别5 Precision=1.0
平均 Precision=0.8888888888888888
手搓 Recall
<math xmlns="http://www.w3.org/1998/Math/MathML"> R e c a l l = T P T P + F N Recall = \frac{TP}{TP+FN} </math>Recall=TP+FNTP
python
mean = 0
for element in classes_list:
# 获取各个类别的 TP TN FP FN
tp, tn, fp, fn = list(element.values())[0].values()
# 计算 Recall
print(f'类别{list(element.keys())[0]} Recall={(tp)/(tp+fn)}')
mean += (tp)/(tp+fn)
print(f'平均 Recall={mean/6}')
==============================
输出:
类别0 Recall=0.6
类别1 Recall=0.6
类别2 Recall=0.6
类别3 Recall=0.6
类别4 Recall=1.0
类别5 Recall=0.6
平均 Recall=0.6666666666666666
手搓 F1
<math xmlns="http://www.w3.org/1998/Math/MathML"> F 1 = 2 ∗ p r e c i s i o n ∗ r e c a l l p r e c i s i o n + r e c a l l F1 = 2 * \frac{precision * recall}{precision + recall} </math>F1=2∗precision+recallprecision∗recall
python
mean = 0
for element in classes_list:
# 获取各个类别的 TP TN FP FN
tp, tn, fp, fn = list(element.values())[0].values()
# 计算 Precision、Recall
precision = (tp)/(tp+fp)
recall = (tp)/(tp+fn)
print(f'类别{list(element.keys())[0]} F1={2*(precision*recall)/(precision+recall)}')
mean+=2*(precision*recall)/(precision+recall)
print(f'平均 F1={mean/6}')
==============================
输出:
类别0 F1=0.7499999999999999
类别1 F1=0.7499999999999999
类别2 F1=0.7499999999999999
类别3 F1=0.7499999999999999
类别4 F1=0.5
类别5 F1=0.7499999999999999
平均 F1=0.7083333333333331