简体   繁体   English

NumPy 的误报率 (FPR)

[英]False positive rate (FPR) with NumPy

I have to calculate the false positive rate for multiclass classification using only numpy methods.我必须仅使用 numpy 方法来计算多类分类的误报率。 I have two numpy arrays, one for the predictions ((m, k) shape: m is the count of sample elements and k is the count of categories) and another for the true labels ((m,) shape).我有两个 numpy arrays,一个用于预测((m, k) 形状:m 是样本元素的数量,k 是类别的数量),另一个用于真实标签((m,) 形状)。

What I already did: determine the prediction (positive) element indeces for all the rows (prediction_labels array), making a set for the unique categories (true_labels).我已经做了什么:确定所有行(prediction_labels 数组)的预测(正)元素索引,为唯一类别(true_labels)制作一组。

What I want to do: iterate through the prediction_labels and the y_true arrays in the same time and count whether the given element (each unique value in the true_labels) is equal in the same position. So I want to determine the false positive counts by category in an array (false_positive_counts)我想做的是:同时遍历 prediction_labels 和 y_true arrays 并计算给定元素(true_labels 中的每个唯一值)在同一个 position 中是否相等。所以我想按类别确定误报计数在数组中 (false_positive_counts)

For example:例如:

def false_positive_rate(y_pred, y_true):
    prediction_labels = np.argmax(y_pred, axis=1)
    true_labels = np.unique(y_true)
    false_positive_counts = ... # ?
    ...
    return fpr

y_pred = np.array([[1., 0., 0., 0.],
                   [1., 0., 0., 0.], 
                   [0., 0., 1., 0.],
                   [0., 0., 1., 0.],
                   [0., 1., 0., 0.],
                   [0., 0., 0., 1.],
                  ])  # [0,0,2,2,1,3]
y_true = np.array([0, 2, 1, 1, 1, 3])
print(false_positive_rate(y_pred, y_true))   # 3/20

You have positive and negative in predictions.您的预测有正面和负面。 But, there is no False in answers.但是,答案中没有错误。

Therefore, your FPR is always 1.因此,您的 FPR 始终为 1。

def false_positive_rate(y_pred_raw, y_true):
    y_pred = np.argmax(y_pred_raw, axis=1)
    TP, FP, FN, TN = 0,0,0,0
    for pp, tt in zip(y_pred, y_true):
        if   pp==tt: TP+=1;
        elif pp!=tt: FP+=1;
        # there is no case for FN, TN
    print(f"TP={TP}, FP={FP}, FN={FN}, TN={TN}");
    FPR = FP/ (TN+FP);
    return FPR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM