首页 > 解决方案 > 带有百分位数的 ROC 曲线

问题描述

我想绘制 ROC 曲线(假阳性率与中值真阳性率)以及两个范围:

  1. 高分位数范围(高分位数 TPR 与 FPR),例如 85% 分位数
  2. 低分位数范围(低分位数 TPR 与 FPR),例如 25% 分位数

我的误报率定义为:

def false_positive_rate(preds, targets):
    preds = np.array(preds)
    targets = np.array(targets)
    ids = np.argsort(-preds)
    sorted_targets = targets[ids]
    P = np.sum(targets == 1)
    N = np.sum(targets != 1)
    FPR = np.cumsum(1 - sorted_targets)/N
    return FPR

真阳性率:

def true_positive_rate(preds, targets):
    preds = np.array(preds)
    targets = np.array(targets)
    ids = np.argsort(-preds)
    sorted_targets = targets[ids]
    P = np.sum(targets == 1)
    N = np.sum(targets != 1)
    TPR = np.cumsum(sorted_targets)/P
    return TPR

计算 FPR、高、低和中 TPR:

probs = clf.predict_proba(X_test)
probs = probs[:, 1]

num = 100
scores_list = []
for n in range(num):
    clf = train_func(X_train, y_train, param)
    probs = score_func(X_test, clf)
    scores_list.append(probs)

fpr = false_positive_rate(scores_list, y_test)
tpr_low = true_positive_rate_func(np.percentile(scores_list, 25, axis=0), y_test)
tpr_mid = true_positive_rate_func(np.percentile(scores_list, 50, axis=0), y_test)
tpr_high = true_positive_rate_func(np.percentile(scores_list, 85, axis=0), y_test)

这是计算百分位数的正确方法吗?我没有得到想要的 ROC 图。

标签: pythonmachine-learningrocpercentile

解决方案


推荐阅读