如何在Python中不使用sklearn计算TPR和FPR?

Sah*_*boj 3 python machine-learning roc auc precision-recall

初始化列表列表:

data = [[1.0, 0.635165,0.0], [1.0, 0.766586,1.0], [1.0, 0.724564,1.0],
        [1.0, 0.766586,1.0],[1.0, 0.889199,1.0],[1.0, 0.966586,1.0],
        [1.0, 0.535165,0.0],[1.0, 0.55165,0.0],[1.0, 0.525165,0.0],
        [1.0, 0.5595165,0.0] ]
Run Code Online (Sandbox Code Playgroud)

创建 Pandas 数据框:

df = pd.DataFrame(data, columns = ['y', 'prob','y_predict']) 
Run Code Online (Sandbox Code Playgroud)

打印数据框。

print(df)
Run Code Online (Sandbox Code Playgroud)

对于这个数据集,我想找到:

  1. 不使用 Sklearn 的混淆矩阵
  2. 不使用 Sklearn 的 TPR 和 FPR 的 Numpy 数组,用于绘制 ROC。

如何在 python 中做到这一点?

Fla*_*ino 10

您可以计算与不同阈值水平相关的误报率和真报率,如下所示:

import numpy as np

def roc_curve(y_true, y_prob, thresholds):

    fpr = []
    tpr = []

    for threshold in thresholds:

        y_pred = np.where(y_prob >= threshold, 1, 0)

        fp = np.sum((y_pred == 1) & (y_true == 0))
        tp = np.sum((y_pred == 1) & (y_true == 1))

        fn = np.sum((y_pred == 0) & (y_true == 1))
        tn = np.sum((y_pred == 0) & (y_true == 0))

        fpr.append(fp / (fp + tn))
        tpr.append(tp / (tp + fn))

    return [fpr, tpr]
Run Code Online (Sandbox Code Playgroud)