NumPy 的误报率 (FPR)

发布于 2025-01-17 05:10:17 字数 890 浏览 0 评论 0原文

我必须仅使用 numpy 方法来计算多类分类的误报率。我有两个 numpy 数组,一个用于预测((m, k) 形状:m 是样本元素的计数,k 是类别的计数),另一个用于真实标签((m,) 形状)。

我已经做了什么:确定所有行(prediction_labels 数组)的预测(正)元素 indeces,为唯一类别(true_labels)制作一组。

我想要做的:同时迭代 Prediction_labels 和 y_true 数组,并计算给定元素(true_labels 中的每个唯一值)在同一位置是否相等。所以我想按数组中的类别确定误报计数 (false_positive_counts)

例如:

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

I have to calculate the false positive rate for multiclass classification using only numpy methods. 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).

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).

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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱&纷飞 2025-01-24 05:10:17

你的预测有积极的和消极的。
但是,答案中没有错误。

因此,你的 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

You have positive and negative in predictions.
But, there is no False in answers.

Therefore, your FPR is always 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文