ROC曲线的绘制
- roc_curve
-
ROC曲线指受试者工作特征曲线/接收器操作特性(receiver operating characteristic,ROC)曲线,是反映灵敏性和特效性连续变量的综合指标,是用构图法揭示敏感性和特异性的相互关系,它通过将连续变量设定出多个不同的临界值,从而计算出一系列敏感性和特异性。ROC曲线是根据一系列不同的二分类方式(分界值或决定阈),以真正例率(也就是灵敏度)(True Positive Rate,TPR)为纵坐标,假正例率(1-特效性)(False Positive Rate,FPR)为横坐标绘制的曲线。
ROC观察模型正确地识别正例的比例与模型错误地把负例数据识别成正例的比例之间的权衡。TPR的增加以FPR的增加为代价。ROC曲线下的面积是模型准确率的度量,AUC(Area under roccurve)。
纵坐标:真正率(True Positive Rate , TPR)或灵敏度(sensitivity)
TPR = TP /(TP + FN) (正样本预测结果数 / 正样本实际数)
横坐标:假正率(False Positive Rate , FPR)
FPR = FP /(FP + TN) (被预测为正的负样本结果数 /负样本实际数)
- 形式:
sklearn.metrics.roc_curve(y_true,y_score, pos_label=None, sample_weight=None, drop_intermediate=True)
该函数返回这三个变量:fpr,tpr,和阈值thresholds;
这里理解thresholds:
分类器的一个重要功能“概率输出”,即表示分类器认为某个样本具有多大的概率属于正样本(或负样本)。
“Score”表示每个测试样本属于正样本的概率。
接下来,我们从高到低,依次将“Score”值作为阈值threshold,当测试样本属于正样本的概率大于或等于这个threshold时,我们认为它为正样本,否则为负样本。每次选取一个不同的threshold,我们就可以得到一组FPR和TPR,即ROC曲线上的一点。当我们将threshold设置为1和0时,分别可以得到ROC曲线上的(0,0)和(1,1)两个点。将这些(FPR,TPR)对连接起来,就得到了ROC曲线。当threshold取值越多,ROC曲线越平滑。其实,我们并不一定要得到每个测试样本是正样本的概率值,只要得到这个分类器对该测试样本的“评分值”即可(评分值并不一定在(0,1)区间)。评分越高,表示分类器越肯定地认为这个测试样本是正样本,而且同时使用各个评分值作为threshold。我认为将评分值转化为概率更易于理解一些。
- >>>import numpy as np
- >>>from sklearn import metrics
- >>>y = np.array([1, 1, 2, 2])
- >>>scores = np.array([0.1, 0.4, 0.35, 0.8])
- >>>fpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=2)
- >>>fpr
- array([0. , 0.5, 0.5, 1. ])
- >>>tpr
- array([0.5, 0.5, 1. , 1. ])
- >>>thresholds
- array([0.8 , 0.4 , 0.35, 0.1 ])
- >>>from sklearn.metrics import auc
- >>>metrics.auc(fpr, tpr)
- 0.75
-
sklearn上有一个画ROC曲线的例子,利用的是经典的鸢尾花(iris)数据。但鸢尾花数据分类的结果有三种,例子就直接来做图(一般的分类任务明明只有两种结果啊!!!),对于初学者来说(说的是我自己)看起来真的别扭。因此我对该例子做了一些改动(简化),将数据转化为二分类,这样比较容易理解。
首先为大家介绍一下Python做ROC曲线的原理。sklearn.metrics有roc_curve, auc两个函数,ROC曲线上的点主要就是通过这两个函数计算出来的。
(1.)
fpr, tpr, thresholds = roc_curve(y_test, scores)
其中y_test为测试集的结果,scores为模型预测的测试集得分(注意:通过decision_function(x_test)计算scores的值);fpr,tpr,thresholds 分别为假正率、真正率和阈值。(应该是不同阈值下的真正率和假正率)。
(2.)
roc_auc =auc(fpr, tpr)
roc_auc为计算的acu的值。
PS:下面的代码大家可以直接copy跑一下,我相信跑完之后大家就理解作图的原理了。
- # -*- coding: utf-8 -*-
- """
- Created on Thu Sep 21 16:13:04 2017
- @author: lizhen
- """
- import numpy as np
- import matplotlib.pyplot as plt
- from sklearn import svm, datasets
- from sklearn.metrics import roc_curve, auc ###计算roc和auc
- from sklearn import cross_validation
- # Import some data to play with
- iris = datasets.load_iris()
- X = iris.data
- y = iris.target
- ##变为2分类
- X, y = X[y != 2], y[y != 2]
- # 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 = cross_validation.train_test_split(X, y, test_size=.3,random_state=0)
- # Learn to predict each class against the other
- svm = svm.SVC(kernel='linear', probability=True,random_state=random_state)
- ###通过decision_function()计算得到的y_score的值,用在roc_curve()函数中
- y_score = svm.fit(X_train, y_train).decision_function(X_test)
- # Compute ROC curve and ROC area for each class
- fpr,tpr,threshold = roc_curve(y_test, y_score) ###计算真正率和假正率
- roc_auc = auc(fpr,tpr) ###计算auc的值
- plt.figure()
- lw = 2
- plt.figure(figsize=(10,10))
- plt.plot(fpr, tpr, color='darkorange',
- lw=lw, label='ROC curve (area = %0.2f)' % roc_auc) ###假正率为横坐标,真正率为纵坐标做曲线
- 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()
-