分类模型评估

# 分类模型评估

- sklearn.metrics.accuracy_score
- 定义:分类的准确率

- 在多标签分类中,此函数计算子集的准确性为:样本预测的标签集必须与y_true中的相应标签集*完全*匹配

- 参数

- y_true
- 样本的真实值 形状:1d array-like
- y_pred
- 样本的预测值 形状:1d array-like
- normalize
- False :返回预测正确的样本数量 True:返回预测正确的样本百分比
- sample_weight
- 样本权重

- 例子

```
from sklearn.metrics import accuracy_score

y_true = [0, 1, 2, 3]
y_pred = [0, 2, 1, 3]
normalize = False
sample_weight = [1, 2, 3, 2]

res = accuracy_score(y_true=y_true, y_pred=y_pred, normalize=normalize, sample_weight=sample_weight)
print(res)

---------
3

```

- 计算过程
- y_true == y_pred = [1,0,0,1]
- 1 * 1+0 * 2+0 * 3+1 * 2 = 3

- sklearn.metrics.balanced_accuracy_score

- 针对样本各个分类的平均准确率

- 参数

- y_true
- 样本的真实值 形状:1d array-like
- y_pred
- 样本的预测值 形状:1d array-like
- adjusted
- 如果为true,则对结果进行机会调整,以便随机性能得分为0,而完美性能得分为1
- sample_weight
- 样本权重

- 例子

```
from sklearn.metrics import accuracy_score,balanced_accuracy_score

y_true = [0, 1, 2, 0, 0]
y_pred = [0, 2, 2, 0, 1]
normalize = False
sample_weight = [1, 2, 3, 2, 5]

res = balanced_accuracy_score(y_true=y_true, y_pred=y_pred, sample_weight=sample_weight,adjusted=False)
print(res)
res = balanced_accuracy_score(y_true=y_true, y_pred=y_pred, sample_weight=sample_weight,adjusted=True)
print(res)
----------------
0.4583333333333333
0.18749999999999997
```

- 计算过程
- y_true == y_pred = [1, 0, 1, 1, 0]
- y_true [ 0 ,1, 2 ,0, 0]
- sample_weight = [1, 2, 3, 2, 5]
- 各个样本[ 0 , 1, 2 ]种类的预测的准确率为 [(1x1+1x2+0x5)/(1+2+5),(0x2)/2,(1x3)/3]
- 各个样本[ 0 , 1, 2 ]种类的预测的准确率为[3/8,0,1]
- 共有三种样本
- adjusted = False 平均样本准确率为 (3/8+0+1)/3 = 0.4583
- adjusted = True
- 平均样本准确率为 (3/8+0+1)/3 = 0.4583
- 0.4583 - (1/3) = 0.125
- 0.125/(1-1/3) = 0.188

原文地址:https://www.cnblogs.com/lycsdhr/p/13623986.html