来源及说明

来源于 sklearn 库的 metrics.roc_curve 主要用来计算ROC曲线面积。

sklearn.metrics.roc_curve(y_true, y_score, *, pos_label=None, sample_weight=None, drop_intermediate=True)
输入参数:
    y_true : ndarray of shape (n_samples,)
         #真实的二进制标签。如果标签不是{-1,1}或{0,1},那么pos_label应该明确给出。
    y_score : ndarray of shape (n_samples,)
	#可以是正类的概率估计值,也可以是可信度值,或者是非正类的类的概率估计值,信心值,或者非阈值的决策测量值(如某些分类器上的 "decision_function "所返回的)。
    pos_label : int or str, default=None
      # 正面类的标签。 当`pos_label=None'时,如果`y_true'在{-1, 1}或{0, 1}。`pos_label'会被设置为1,否则会出现错误。
    sample_weight : # 类似数组的形状(n_samples,), default=None 表示样本权重。
    drop_intermediate : bool, default=True
# 是否放弃一些不会出现在ROC曲线上的次优阈值。在绘制的ROC曲线上。这对于创建较轻的ROC曲线很有用。
输出参数:
    fpr : ndarray of shape (>2,)
         #递增的假阳性率,其中i元素是score >= `thresholds[i]`.的预测的假阳性率。 
    tpr : ndarray of shape (>2,)
        #增加真实阳性率,元素i是预测的真实阳性率,score >= `thresholds[i]`的预测的真阳性率。
    thresholds : ndarray of shape = (n_thresholds,)
        #用于计算决策函数的递减。`thresholds[0]` 代表没有被预测的实例。并被任意设置为`max(y_score) + 1'。
绘制ROC曲线:
        plt.title('ROC')
        plt.xlabel('False Positive Rate')
        plt.ylabel('True Positive Rate')
        plt.plot(fpr, tpr, '--*b', label="ours")
        plt.legend()
        plt.show()

TP(True Positives),TN(True Negatives),FP(False Positives),FN(False Negatives) 分别为阳性样本正确分类数量、阴性样本正确分类数量、阳性样本错误分类数量以及阴性样本错误分类数量。

阳性样本正确分类数量占阳性样本总数的比例TPR即True Positive Rate,所有正类中,有多少被预测成正类),阴性样本正确分类的数量占阴性样本总数的比例FPR即False Positive Rate,所有反类中,有多少被预测成正类计算如下:

  T P R = T P / ( T P + F N ) \ TPR = TP/(TP+FN)  TPR=TP/(TP+FN)

(阳性样本被判断为阳性/阳性样本总数)

  F P R = F P / ( F P + T N ) \ FPR = FP/(FP+TN)  FPR=FP/(FP+TN)

(阴性样本被判断为阳性/阴性样本总数)

ROC曲线横坐标为FPR,纵坐标为TPR。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐