首页 > 技术文章 > sklearn.metrics中的评估方法

gaowenxingxing 2020-02-13 16:15 原文

https://www.cnblogs.com/mindy-snail/p/12445973.html

1.confusion_matrix

  • 利用混淆矩阵进行评估

  • 混淆矩阵说白了就是一张表格-

  • 所有正确的预测结果都在对角线上,所以从混淆矩阵中可以很方便直观的看出哪里有错误,因为他们呈现在对角线外面。

  • 举个直观的例子

    这个表格是一个混淆矩阵

正确的值是上边的表格,混淆矩阵是下面的表格,这就表示,apple应该有两个,但是只预测对了一个,其中一个判断为banana了,banana应该有8ge,但是5个预测对了3个判断为pear了,pear有应该有6个,但是2个判断为apple了,可见对角线上是正确的预测值,对角线之外的都是错误的。
这个混淆矩阵的实现代码

from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
y_test=["a","b","p","b","b","b","b","p","b","p","b","b","p","p","p","a"]
y_pred=["a","b","p","p","p","p","b","p","b","p","b","b","a","a","p","b"]
confusion_matrix(y_test, y_pred,labels=["a", "b","p"])
#array([[1, 1, 0],
       [0, 5, 3],
       [2, 0, 4]], dtype=int64)

print(classification_report(y_test,y_pred))
##
               precision    recall  f1-score   support

          a       0.33      0.50      0.40         2
          b       0.83      0.62      0.71         8
          p       0.57      0.67      0.62         6

avg / total       0.67      0.62      0.64        16

我传到github上面了

复现代码1

# Import necessary modules
from sklearn.metrics import classification_report

from sklearn.metrics import confusion_matrix

# Create training and test set
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.4,random_state=42)

# Instantiate a k-NN classifier: knn
knn = KNeighborsClassifier(6)

# Fit the classifier to the training data
knn.fit(X_train,y_train)

# Predict the labels of the test data: y_pred
y_pred = knn.predict(X_test)

# Generate the confusion matrix and classification report
print(confusion_matrix(y_test,y_pred))
print(classification_report(y_test,y_pred))

复现代码2

补充知识

先给一个二分类的例子
其他同理

  • TP(True Positive):将正类预测为正类数,真实为0,预测也为0
  • FN(False Negative):将正类预测为负类数,真实为0,预测为1
  • FP(False Positive):将负类预测为正类数, 真实为1,预测为0
  • TN(True Negative):将负类预测为负类数,真实为1,预测也为1

因此:预测性分类模型,肯定是希望越准越好。那么,对应到混淆矩阵中,那肯定是希望TP与TN的数量大,而FP与FN的数量小。所以当我们得到了模型的混淆矩阵后,就需要去看有多少观测值在第二、四象限对应的位置,这里的数值越多越好;反之,在第一、三四象限对应位置出现的观测值肯定是越少越好。

几个二级指标定义

  • 准确率(Accuracy)—— 针对整个模型
    \(\frac{t p+t n}{t p+t n+f p+f n}\)
  • 精确率(Precision)
    \(\frac{t p}{t p+f n}\)
  • 灵敏度(Sensitivity):就是召回率(Recall)召回率 = 提取出的正确信息条数 / 样本中的信息条数。通俗地说,就是所有准确的条目有多少被检索出来了
  • 特异度(Specificity)

三级指标

\(\mathrm{F} 1\) Score \(=\frac{2 \mathrm{PR}}{\mathrm{P}+\mathrm{R}}\)
其中,P代表Precision,R代表Recall。
F1-Score指标综合了Precision与Recall的产出的结果。F1-Score的取值范围从0到1的,1代表模型的输出最好,0代表模型的输出结果最差reference

2.accuracy_score()

分类准确率分数

  • 分类准确率分数是指所有分类正确的百分比。分类准确率这一衡量分类器的标准比较容易理解,但是它不能告诉你响应值的潜在分布,并且它也不能告诉你分类器犯错的类型
sklearn.metrics.accuracy_score(y_true, y_pred, normalize=True, sample_weight=None)
#normalize:默认值为True,返回正确分类的比例;如果为False,返回正确分类的样本数

复现代码1

#accuracy_score
import numpy as np
from sklearn.metrics import accuracy_score
y_pred = [1, 9, 9, 5,1,0,2,2]
y_true = [1,9,9,8,0,6,1,2]
print(accuracy_score(y_true, y_pred))
print(accuracy_score(y_true, y_pred, normalize=False))
# 0.5
# 4

复现代码2

datacamp上面的一个例子

# Import necessary modules
from sklearn.neighbors import KNeighborsClassifier 
from sklearn.model_selection import train_test_split

# Create feature and target arrays
X = digits.data
y = digits.target

# Split into training and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state=42, stratify=y)

# Create a k-NN classifier with 7 neighbors: knn
knn = KNeighborsClassifier(n_neighbors=7)

# Fit the classifier to the training data
knn.fit(X_train, y_train)
y_pred=knn.predict(X_test)
# Print the accuracy
print(accuracy_score(y_test, y_pred))

#0.89996709

ROC

  • ROC曲线指受试者工作特征曲线/接收器操作特性(receiveroperating characteristic,ROC)曲线,
  • 是反映灵敏性和特效性连续变量的综合指标,是用构图法揭示敏感性和特异性的相互关系,
  • 它通过将连续变量设定出多个不同的临界值,从而计算出一系列敏感性和特异性。
  • ROC曲线是根据一系列不同的二分类方式(分界值或决定阈),以真正例率(也就是灵敏度recall)(True Positive Rate,TPR)为纵坐标,假正例率(1-特效性,)(False Positive Rate,FPR)为横坐标绘制的曲线。
  • 要与混淆矩阵想结合

横轴FPR

\(\mathrm{FPR}=\frac{\mathrm{FP}}{\mathrm{FP}+\mathrm{TN}}\)
在所有真实值为Negative的数据中,被模型错误的判断为Positive的比例

如果两个概念熟,那就多看几遍

推荐阅读