准确率得分:计算标准、平衡和每类的准确率

一个函数用于计算基础分类准确率、每类准确率和每类平均准确率。

> 从 mlxtend.evaluate 导入准确率评分

示例 1 -- 标准准确度

“整体”准确率定义为正确预测(真正 TP 和 真负 TN)在所有样本 n 中的比例:

$$ACC = \frac{TP + TN}{n}$$

import numpy as np
from mlxtend.evaluate import accuracy_score


y_targ = [0, 0, 0, 1, 1, 1, 2, 2, 2]
y_pred = [1, 0, 0, 0, 1, 2, 0, 2, 2]

accuracy_score(y_targ, y_pred)

0.5555555555555556

示例 2 -- 按类别准确率

每个类别的准确率是一个类别(定义为 pos_label)与数据集中所有剩余数据点之间的准确率。

import numpy as np
from mlxtend.evaluate import accuracy_score


y_targ = [0, 0, 0, 1, 1, 1, 2, 2, 2]
y_pred = [1, 0, 0, 0, 1, 2, 0, 2, 2]

std_acc = accuracy_score(y_targ, y_pred)
bin_acc = accuracy_score(y_targ, y_pred, method='binary', pos_label=1)

print(f'Standard accuracy: {std_acc*100:.2f}%')
print(f'Class 1 accuracy: {bin_acc*100:.2f}%')

Standard accuracy: 55.56%
Class 1 accuracy: 66.67%

示例 3 -- 每类平均准确率

概述

“整体”准确性定义为所有样本中正确预测的数量(真正 TP 和 真负 TN)与总样本数 n 的比值:

$$ACC = \frac{TP + TN}{n}$$

在二分类设置中:

在多类别设置中,我们可以将准确率的计算推广为所有真实预测(对角线)的比例与所有样本 n 之比。

$$ACC = \frac{T}{n}$$

考虑一个有 3 个类别 (C0, C1, C2) 的多类别问题

假设我们的模型做出了以下预测:

我们计算准确率为:

$$ACC = \frac{3 + 50 + 18}{90} \approx 0.79$$

现在,为了计算每类的平均准确率,我们分别计算每个类别标签的二元准确率;即,如果类别1是积极类别,则类别0和2都被视为消极类别。

$$APC\;ACC = \frac{83/90 + 71/90 + 78/90}{3} \approx 0.86$$

import numpy as np
from mlxtend.evaluate import accuracy_score


y_targ = [0, 0, 0, 1, 1, 1, 2, 0, 0]
y_pred = [1, 0, 0, 0, 1, 2, 0, 2, 1]

std_acc = accuracy_score(y_targ, y_pred)
bin_acc = accuracy_score(y_targ, y_pred, method='binary', pos_label=1)
avg_acc = accuracy_score(y_targ, y_pred, method='average')

print(f'Standard accuracy: {std_acc*100:.2f}%')
print(f'Class 1 accuracy: {bin_acc*100:.2f}%')
print(f'Average per-class accuracy: {avg_acc*100:.2f}%')

Standard accuracy: 33.33%
Class 1 accuracy: 55.56%
Average per-class accuracy: 55.56%

参考文献

API

accuracy_score(y_target, y_predicted, method='standard', pos_label=1, normalize=True)

General accuracy function for supervised learning. Parameters

Returns

score: float

Examples

For usage examples, please see https://rasbt.github.io/mlxtend/user_guide/evaluate/accuracy_score/