NumPy 的误报率 (FPR)
我必须仅使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的预测有积极的和消极的。
但是,答案中没有错误。
因此,你的 FPR 始终为 1。
You have positive and negative in predictions.
But, there is no False in answers.
Therefore, your FPR is always 1.