对于二分类模型,其实既可以构建分类器,也可以构建回归(比如同一个二分类问题既可以用SVC又可以SVR,python的sklearn中SVC和SVR是分开的,R的e1701中都在svm中,仅当y变量是factor类型时构建SVC,否则构建SVR)。

二分类模型的评价指标很多,这里仅叙述AUC这个指标。分类问题中,正类预测score大于负类预测score的概率即是C指数(Mann–Whitney U检验的C统计量),也称AUC,推导可以参考:AUC与Mann–Whitney U test 以及 wikipedia/Mann_Whitney_U_test。AUC的具体原理此处不再叙述,可以参考相关资料,比如这两个还行:ROC和AUC介绍以及如何计算AUC、机器学习之分类性能度量指标,以及这两个也有些不错的思考:AUC原理介绍及求解方法总结。

其实,ROC的另一种呈现可以是直方图(或密度图)形式,即横坐标是score,直方图(密度图)的颜色是真实类别,比如下面这种:

若构建regression,可以直接将predict的值和真实值直接扔进auc函数里去计算,就是让程序去逐个找predict的cutoff值就可以构建ROC了。

但是如果是classifier,因为直接predict的值是0或1,无法计算auc,此时需要借助于“预测概率”,sklearn中常调用predict_proba函数来获取。另外,Logistics回归,python的predict也是0或1,也需要调用predict_proba函数来获取相应“预测概率”。还有个decision_function,其意义是当这个值大于0时,相应的样本预测为正例。R中不会有这些问题,R都是简单易用的。

AUC的计算举例:

test_auc = metrics.roc_auc_score(y_test,y_test_pre)

ROC的计算举例:

fpr, tpr, thresholds = metrics.roc_curve(y_test,y_test_pre)
plt.plot(fpr, tpr, 'b', label='AUC = %0.2f' % test_auc)

Classifier的其他相关评价指标

准确度accuracy:可以用classifier.score计算accuracy。理解为正确率,就是分类正确的占总数的比例,即(TP+TN)/Total。

二分类问题中,当其中某一类数量远小于另一类时,如果追求准确度,那么只需要将分类结果全指定为数量多的那类即可。所以这种情况下仅用accuracy评价是不够的。

精确度precision:又叫“查准率”、用P表示。这是针对其中一类而言。比如我建模的目的是找出正例,那么precision就是真正的正例/找出来的所有,即TP/(TP+FP)。

召回率recall:又叫“查全率”、“灵敏度”、“真阳性率TPR”,用R表示。也是针对其中一类而言。比如建模的目的是找出正例,那么recall就是真正的正例/所有的正例,即TP/(TP+FN)。另,假阴性率FNR(漏诊率)=FN/(TP+FN),FNR=1-R。

真阴性率TNR:又叫“特异度”,TNR=TN/(TN+FP)。假阳性率FPR:又叫“误诊率”。TNR+FPR=1。(还记得ROC的横坐标吗)

总而言之,准确率就是找得对,召回率就是找得全。

关于灵敏度和特异度的关系:如果说灵敏度为正例的召回率,那么特异度就是反例的召回率。当正例和反例同样重要时,灵敏度和特异度的重要性也一样。所以,在无偏的分类模型中(各类的重要性相同),由灵敏度和特异度组合的指标(如AUC)是十分重要的指标。注意,当模型是为了找出特殊人群(比如找出高危人群)时,识别特殊人群的灵敏度,要比特异度更重要。

你问问一个模型,这堆东西是不是某个类的时候,准确率就是 它说是,这东西就确实是的概率吧,召回率就是, 它说是,但它漏说了(1-召回率)这么多

F1值:是精确值和召回率的调和均值,即 2/F1=1/precision+1/recall。Fβ是更一般的形式,对precision和recall加权。而F1是其特殊情况,认为precision和recall同等重要。推广的话还有macro-P、macro-R、macro-F1及micro-P、micro-R、micro-F1等。

贴张图(来自:机器学习】分类性能度量指标 : ROC曲线、AUC值、正确率、召回率、敏感度、特异度)

另外wiki上也有张图:

准确率和召回率是互相影响的,理想情况下肯定是做到两者都高,但是一般情况下准确率高、召回率就低,召回率低、准确率高,当然如果两者都低,那是什么地方出问题了

如果是做搜索,那就是保证召回的情况下提升准确率;如果做疾病监测、反垃圾,则是保准确率的条件下,提升召回。

所以,在两者都要求高的情况下,可以用F1来衡量。

P/R和ROC是两个不同的评价指标和计算方式,一般情况下,检索用前者,分类、识别等用后者。

参考一篇还不错的博客:【机器学习】分类性能度量指标 : ROC曲线、AUC值、正确率、召回率、敏感度、特异度

一般认为,AUC<0.6时区分度较差,0.6-0.75时区分能力一般,>7.5时区分能力较好。

两个模型的AUC是可以进行统计学差异检验的,采用Z检验,统计量Z近似服从正态分布。计算公式如下:

SE1和SE2分别为AUC1和AUC2的标准误。

贴几段代码

# SVR与SVC的AUC/ROC计算
import numpy as np
from sklearn.svm import SVR,SVC
from sklearn.model_selection import train_test_split
from sklearn import metricsx_train, x_test, y_train, y_test = train_test_split(X, Y,  train_size=0.7)print("------------------------------ SVC ------------------------------------------")
clf = SVC(kernel='rbf', C=100, gamma=0.0001, probability=True)
clf.fit(x_train, y_train)y_train_pre = clf.predict(x_train)
y_test_pre = clf.predict(x_test)
print("Accuracy: "+str(clf.score(x_train,y_train)))  y_train_predict_proba = clf.predict_proba(x_train) #每一类的概率
false_positive_rate, recall, thresholds = roc_curve(y_train, y_train_predict_proba[:, 1])
train_auc=auc(false_positive_rate,recall)
print("train AUC: "+str(train_auc))print("------------------------------------")
print("Accuracy: "+str(clf.score(x_test,y_test)))y_test_predict_proba = clf.predict_proba(x_test) #每一类的概率
false_positive_rate, recall, thresholds = roc_curve(y_test, y_test_predict_proba[:, 1])
test_auc=auc(false_positive_rate,recall)
print("test AUC: "+str(test_auc))plt.figure(0)
plt.title('ROC of SVM in test data')
plt.plot(false_positive_rate, recall, 'b', label='AUC = %0.2f' % test_auc)
plt.legend(loc='lower right')
plt.plot([0,1],[0,1],'r--')
plt.xlim([0.0,1.0])
plt.ylim([0.0,1.0])
plt.ylabel('Recall')
plt.xlabel('Fall-out')
plt.show()print("--------------------------- SVR ------------------------------------------")reg = SVR(kernel='rbf', C=100, gamma=0.0001)
reg.fit(x_train, y_train)
y_train_pre = reg.predict(x_train)
y_test_pre = reg.predict(x_test)
train_auc = metrics.roc_auc_score(y_train,y_train_pre)
print("train AUC: "+str(train_auc))print("--------------------------------")test_auc = metrics.roc_auc_score(y_test,y_test_pre)
print("test AUC: "+str(test_auc))
fpr, tpr, thresholds = metrics.roc_curve(y_test,y_test_pre)plt.figure(1)
plt.title('ROC of SVR in test data')
plt.plot(fpr, tpr, 'b', label='AUC = %0.2f' % test_auc)
plt.legend(loc='lower right')
plt.plot([0,1],[0,1],'r--')
plt.xlim([0.0,1.0])
plt.ylim([0.0,1.0])
plt.ylabel('Recall')
plt.xlabel('Fall-out')
plt.show()

Logistics回归代码段

# Logistics regression
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_curve,auc
from sklearn.model_selection import train_test_split# input X、yX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
# clf = LogisticRegression(random_state=0, solver='lbfgs' ,multi_class='multinomial')
clf = LogisticRegression()
clf.fit(x_train, y_train)# 一下几行仅用于展现那几个函数的作用,实际使用不应随便挑几个数据验证
logi_pre=clf.predict(X[:5, :])
logi_pro=clf.predict_proba(X[:5, :])
logi_accuracy=clf.score(x_test, y_test)
logi_deci=clf.decision_function(X[-5:,:])
print(y)
print("prediction of first 5 samples: ",end=" ")
print(logi_pre)
print("prediction probability of first 5 samples: ")
print(logi_pro)
print("decision_function of last 5 samples(大于0时,正类被预测): ",end=" ")
print(logi_deci)
print("prediction accuracy of test data: ",end=" ")
print(logi_accuracy)predictions=clf.predict_proba(x_test)#每一类的概率
false_positive_rate, recall, thresholds = roc_curve(y_test, predictions[:, 1])
roc_auc=auc(false_positive_rate,recall)
plt.title('ROC of logistics in test data')
plt.plot(false_positive_rate, recall, 'b', label='AUC = %0.2f' % roc_auc)
plt.legend(loc='lower right')
plt.plot([0,1],[0,1],'r--')
plt.xlim([0.0,1.0])
plt.ylim([0.0,1.0])
plt.ylabel('Recall')
plt.xlabel('Fall-out')
plt.show()

AUC/ROC计算的sklearn官网举例

print(__doc__)
# ROC for modelimport numpy as np
import matplotlib.pyplot as plt
from itertools import cyclefrom sklearn import svm, datasets
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import label_binarize
from sklearn.multiclass import OneVsRestClassifier
from scipy import interp# Import some data to play with
iris = datasets.load_iris()
X = iris.data
y = iris.target# Binarize the output
y = label_binarize(y, classes=[0, 1, 2])
n_classes = y.shape[1]# Add noisy features to make the problem harder
random_state = np.random.RandomState(0)
n_samples, n_features = X.shape
X = np.c_[X, random_state.randn(n_samples, 200 * n_features)]# shuffle and split training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5,random_state=0)# Learn to predict each class against the other
classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True,random_state=random_state))
y_score = classifier.fit(X_train, y_train).decision_function(X_test)# Compute ROC curve and ROC area for each class
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(n_classes):fpr[i], tpr[i], _ = roc_curve(y_test[:, i], y_score[:, i])roc_auc[i] = auc(fpr[i], tpr[i])# Compute micro-average ROC curve and ROC area
fpr["micro"], tpr["micro"], _ = roc_curve(y_test.ravel(), y_score.ravel())
roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])plt.figure()
lw = 2
plt.plot(fpr[2], tpr[2], color='darkorange',lw=lw, label='ROC curve (area = %0.2f)' % roc_auc[2])
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")
plt.show()

题外话:SVM参数设置的案例

import numpy as np
from sklearn.svm import SVR,SVC
import matplotlib.pyplot as plt# #############################################################################
# Generate sample data
X = np.sort(16 * np.random.rand(80, 1), axis=0)
y = np.sin(X).ravel()# #############################################################################
# Add noise to targets
y[::5] += 3 * (0.5 - np.random.rand(16))# Fit regression model
svr_rbf = SVC(kernel='rbf', C=1e3, gamma=0.1)
# svr_rbf = SVR(kernel='rbf', C=1e3, gamma=100) #可能过拟合
# svr_lin = SVR(kernel='linear', C=1e3)
# svr_poly = SVR(kernel='poly', C=1e3, degree=2)
y_rbf = svr_rbf.fit(X, y).predict(X)# Look at the results
lw = 2
plt.scatter(X, y, color='darkorange', label='data')
plt.plot(X, y_rbf, color='navy', lw=lw, label='RBF model')
# plt.plot(X, y_lin, color='c', lw=lw, label='Linear model')
# plt.plot(X, y_poly, color='cornflowerblue', lw=lw, label='Polynomial model')
plt.xlabel('data')
plt.ylabel('target')
plt.title('Support Vector Regression')
plt.legend()
plt.show()

二分类模型AUC评价法相关推荐

  1. 【Kay】机器学习——二分类模型的评价

    一.评价二分类模型的好坏 二分类问题:预测这条数据是0还是1的问题 1.混淆矩阵 数字代表个数 2.准确率.精确率.召回率 ①准确率: ②精确率(查准率): ③召回率(查全率recall) :   ④ ...

  2. 分类模型-评估指标(2):ROC曲线、 AUC值(ROC曲线下的面积)【只能用于二分类模型的评价】【不受类别数量不平衡的影响;不受阈值取值的影响】【AUC的计算方式:统计所有正负样本对中的正序对】

    评价二值分类器的指标很多,比如precision.recall.F1 score.P-R曲线等.但这些指标或多或少只能反映模型在某一方面的性能.相比而言,ROC曲线则有很多优点,经常作为评估二值分类器 ...

  3. [机器学习] 二分类模型评估指标---精确率Precision、召回率Recall、ROC|AUC

    一 为什么要评估模型? 一句话,想找到最有效的模型.模型的应用是循环迭代的过程,只有通过持续调整和调优才能适应在线数据和业务目标. 选定模型时一开始都是假设数据的分布是一定的,然而数据的分布会随着时间 ...

  4. 衡量二分类模型的统计指标(TN,TP,FN,FP,F1,准确,精确,召回,ROC,AUC)

    文章目录 - 衡量二分类问题的统计指标 分类结果 混淆矩阵 准确率 精确率 召回率 F1评分 推导过程 ROC曲线.AUC - 衡量二分类问题的统计指标 分类结果   二分类问题,分类结果有以下四种情 ...

  5. AI:神经网络IMDB电影评论二分类模型训练和评估

    AI:Keras神经网络IMDB电影评论二分类模型训练和评估,python import keras from keras.layers import Dense from keras import ...

  6. Alink漫谈(八) : 二分类评估 AUC、K-S、PRC、Precision、Recall、LiftChart 如何实现

    Alink漫谈(八) : 二分类评估 AUC.K-S.PRC.Precision.Recall.LiftChart 如何实现 文章目录 Alink漫谈(八) : 二分类评估 AUC.K-S.PRC.P ...

  7. 机器学习(六)分类模型--线性判别法、距离判别法、贝叶斯分类器

    机器学习(六)分类模型--线性判别法.距离判别法.贝叶斯分类器 首先我们了解常见的分类模型和算法有哪些 线性判别法 简单来说就是用一些规定来寻找某一条直线,用直线划分学习集,然后根据待测点在直线的哪一 ...

  8. 机器学习---回归模型和分类模型的评价指标体系

    回归模型评价体系 SSE(误差平方和): R-square(决定系数) Adjusted R-square: 分类模型评价体系 一 ROC曲线和AUC值 二 KS曲线 三 GINI系数 四 Lift ...

  9. 分类模型的评价及比较

    当我们得到数据模型后,该如何评价模型的优劣呢?之前看到过这样一句话 :"尽管这些模型都是错误的,但是有的模型是有用的",想想这句话也是挺有道理的!评价和比较分类模型时,关注的是其泛 ...

  10. RDKit | 基于随机森林的化合物活性二分类模型

    基于随机森林算法的化合物二分类机器学习模型 代码示例 #导入依赖包 import pandas as pd import numpy as np from rdkit import Chem, Dat ...

最新文章

  1. JS的prototype和__proto__
  2. centos安装 ping 命令 ( yum provides )
  3. 秒杀多线程第九篇 经典线程同步总结 关键段 事件 互斥量 信号量
  4. 当代家长现状。。 | 今日最佳
  5. html中的url、href、src的区别
  6. 互联网公司忽悠员工的黑话,套路太深了。
  7. 雷蛇用户计算机不满足系统要求,我的RazerBook13已经升级Windows11了
  8. 舰船目标检测的学习笔记(legacy)
  9. 创建ServiceArea
  10. 床长人工智能教程 - 前言
  11. linux majaro 安装 hp p1106打印机
  12. SNN系列|神经元模型篇(2) Izhikevich
  13. 360实习之--技术基础H卷
  14. python0309
  15. 阿里云VPC网络内网实例通过SNAT连接外网
  16. 初识、初使 MySQL
  17. elasticsearch 性能优化
  18. java发送图片邮件_使用javamail发送包含图片的html格式邮件详解
  19. 阿里云OSS 图片处理api(custom)
  20. 基于Qt、opencv的规则工件尺寸识别

热门文章

  1. Java io流使用相对路径读取文件
  2. 拓扑排序以及求解关键路径
  3. 2008 r2安装总是跳出 server sql_Microsoft SQL Server 2008 R2 安装遇到的问题
  4. 通过温度和湿度计算露点函数
  5. vue 富文本编辑器,插件
  6. c语言自动变量全局变量,C语言全局变量的一些简单介绍
  7. Q 语言初学者系列:(3)Lists 初级
  8. 模糊PI控制的Simulink仿真(保姆级别)
  9. Boost常用库介绍
  10. 使用axure9绘制三级导航