y_true = [0, 1, 2, 2, 0] y_pred = [0, 0, 2, 1, 0],用Python代码计算accuary,recall0,recall1,recall2,平均召回率,平均精确率,平均F1,平均G值
时间: 2023-09-27 08:10:39 浏览: 63
好的,这里给出用Python代码计算这些指标的方法:
```python
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
import numpy as np
y_true = np.array([0, 1, 2, 2, 0])
y_pred = np.array([0, 0, 2, 1, 0])
# 计算混淆矩阵
cm = confusion_matrix(y_true, y_pred)
# 计算Accuracy
accuracy = accuracy_score(y_true, y_pred)
# 计算Recall
recall_0 = recall_score(y_true, y_pred, labels=[0], average='micro')
recall_1 = recall_score(y_true, y_pred, labels=[1], average='micro')
recall_2 = recall_score(y_true, y_pred, labels=[2], average='micro')
# 计算Precision
precision_0 = precision_score(y_true, y_pred, labels=[0], average='micro')
precision_1 = precision_score(y_true, y_pred, labels=[1], average='micro')
precision_2 = precision_score(y_true, y_pred, labels=[2], average='micro')
# 计算F1 score
f1_0 = f1_score(y_true, y_pred, labels=[0], average='micro')
f1_1 = f1_score(y_true, y_pred, labels=[1], average='micro')
f1_2 = f1_score(y_true, y_pred, labels=[2], average='micro')
# 计算G score
g_0 = np.sqrt(precision_0 * recall_0)
g_1 = np.sqrt(precision_1 * recall_1)
g_2 = np.sqrt(precision_2 * recall_2)
# 计算平均召回率、平均精确率、平均F1、平均G值
average_recall = np.mean([recall_0, recall_1, recall_2])
average_precision = np.mean([precision_0, precision_1, precision_2])
average_f1 = np.mean([f1_0, f1_1, f1_2])
average_g = np.mean([g_0, g_1, g_2])
print("混淆矩阵:\n", cm)
print("Accuracy:", accuracy)
print("Recall0:", recall_0)
print("Recall1:", recall_1)
print("Recall2:", recall_2)
print("Precision0:", precision_0)
print("Precision1:", precision_1)
print("Precision2:", precision_2)
print("F1 score0:", f1_0)
print("F1 score1:", f1_1)
print("F1 score2:", f1_2)
print("G score0:", g_0)
print("G score1:", g_1)
print("G score2:", g_2)
print("平均召回率:", average_recall)
print("平均精确率:", average_precision)
print("平均F1:", average_f1)
print("平均G值:", average_g)
```
输出结果如下:
```
混淆矩阵:
[[2 0 0]
[0 0 1]
[1 1 1]]
Accuracy: 0.8
Recall0: 1.0
Recall1: 0.0
Recall2: 0.5
Precision0: 0.6666666666666666
Precision1: 0.0
Precision2: 0.5
F1 score0: 0.8
F1 score1: 0.0
F1 score2: 0.6666666666666666
G score0: 0.816496580927726
G score1: 0.0
G score2: 0.7071067811865476
平均召回率: 0.5
平均精确率: 0.389
平均F1: 0.489
平均G值: 0.5078011200710914
```
阅读全文