利用sklearn做ROC曲线

作者: Ten_Minutes | 来源:发表于2018-04-02 17:01 被阅读251次

    注意:画pr曲线可以参考http://www.cnblogs.com/aquastone/p/random-classifier.html

    对matrix或array型数据做2分类时,如何画出其roc曲线?

    1)首先看一下roc_curve的定义:

    ROC曲线的全称是“受试者工作特性”曲线(Receiver Operating Characteristic),源于二战中用于敌机检测的雷达信号分析技术。是反映敏感性和特异性的综合指标。它通过将连续变量设定出多个不同的临界值,从而计算出一系列敏感性和特异性,再以敏感性为纵坐标、(1-特异性)为横坐标绘制成曲线,曲线下面积越大,判别的准确性越高。在ROC曲线上,最靠近坐标图左上方的点为敏感性和特异性均较高的临界值。

    2)如何作出ROC曲线:

          根据机器学习中分类器的预测得分对样例进行排序,按照顺序逐个把样本作为正例进行预测,计算出FPR和TPR。分别以FPR、TPR为横纵坐标作图即可得到ROC曲线。所以作ROC曲线时,需要先求出FPR和TPR。这两个变量的定义:

    FPR = TP/(TP+FN)

    TPR = TP/(TP+FP)

    TP、FN、FP的定义见下表,表中描述了是一个二分类问题的混淆矩阵:

    fig.1.分类结果混淆矩阵

    表格来源:http://blog.csdn.net/ai_vivi/article/details/43836641

    TP:正确肯定——实际是正例,识别为正例

    FN:错误否定(漏报)——实际是正例,却识别成了负例

    FP:错误肯定(误报)——实际是负例,却识别成了正例

    TN:正确否定——实际是负例,识别为负例

    3)ROC曲线示意图

    fig.2.ROC曲线示意图

          将样本输入分类器,每个样本将得到一个预测得分。我们通过设置不同的截断点,即可截取不同的信息。对应此示例图中,每个阈值的识别结果对应一个点(FPR,TPR)。当阈值取最大时,所有样本都被识别成负样本,对应于坐下角的点(0,0); 当阈值取最小时,所有样本都被识别成正样本,对应于右上角的点(1,1),随着阈值从最大变化到最小,TP和FP都逐渐大;

          那么得到曲线后我们将用什么指标来衡量ROC曲线的好坏呢?这里给出AUC这个指标。AUC表示ROC曲线下方的面积值AUC(Area Under ROC Curve):如果分类器能完美的将样本进行区分,那么它的AUG = 1 ; 如果模型是个简单的随机猜测模型,那么它的AUG = 0.5,对应图中的直线(y=x)。此外,如果一个分类器优于另一个,则它的曲线下方面积相对较大。

    4)如何用python的sklearn画ROC曲线

    sklearn.metrics.roc_curve函数提供了很好的解决方案。

    首先看一下这个函数的用法:

    fpr, tpr, thresholds=

    sklearn.metrics.roc_curve(y_true,y_score,pos_label=None,sample_weight=None,

    drop_intermediate=True)

    参数解析(来源sklearn官网):

    y_true: array, shape = [n_samples]

    True binary labels in range {0, 1} or {-1, 1}. If labels are not binary, pos_label should be explicitly given.

    即真实标签矩阵。

    y_score : array, shape = [n_samples]

    Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers).

    即模型的预测结果矩阵。

    pos_label : int or str, default=None

    Label considered as positive and others are considered negative.

    即标签中认定为正的label个数。

    例如label= [1,2,3,4],如果设置pos_label = 2,则认为3,4为positive,其他均为negtive。

    若label= ['a','a','b','c'], 设置pos_label =’则认为'b'为positive,其他均为negtive。

    sample_weight: array-like of shape = [n_samples]

    optional Sample weights.

    即采样权重,可选择取其中的一部分进行计算。

    drop_intermediate: boolean, optional (default=True)

    Whether to drop some suboptimal thresholds which would not appear on a plotted ROC curve. This is useful in order to create lighter ROC curves.

    即可选择去掉一些对于ROC性能不利的阈值,使得得到的曲线有更好的表现性能。

    返回值Returns:

    thresholds: array, shape = [n_thresholds]

    Decreasing thresholds on the decision function used to compute fpr and tpr. thresholds[0] represents no instances being predicted and is arbitrarily set to max(y_score) + 1.

    即所选择不同的阈值,按照降序排列。

    fpr : array, shape = [>2]

    Increasing false positive rates such that element i is the false positive rate of predictions with score >= thresholds[i].

    根据不同阈值求出的fpr。

    tpr: array, shape = [>2]

    Increasing true positive rates such that element i is the true positive rate of

    predictions with score >= thresholds[i].

    根据不同阈值求出来的tpr上述方法可得到一组tpr和fpr,在此基础上即可作出roc曲线。求AUC可通过函数auc(fpr,tpr),其返回值即为AUC的值。

    5)实例分析如下:

    import numpy as np

    from sklearn import metrics

    import matplotlib.pyplot as plt

    from sklearn.metrics import auc

    y = np.array([1,1,2,3])

    #y为数据的真实标签

    scores = np.array([0.1, 0.2, 0.35, 0.8])

    #scores为分类其预测的得分

    fpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=2)

    #得到fpr,tpr, thresholds

    返回值对应如下:

    得到一组fpr和tpr之后即可画出该次测试对应的roc曲线

    plt.plot(fpr,tpr,marker = 'o')

    plt.show()

    得到ROC曲线:

    fig.4.ROC曲线

    求出AUC:

    from sklearn.metrics import auc

    AUC = auc(fpr, tpr)

    最终得到AUC=0.67

    相关文章

      网友评论

        本文标题:利用sklearn做ROC曲线

        本文链接:https://www.haomeiwen.com/subject/chyxhftx.html