将 NumPy 数组与另一个数组进行比较

时间:2021-06-25 06:45:39

标签: numpy

我有 2 个 NumPy 数组,例如:

correct   = np.array([1, 1, 1, 1, 1, 2, 2, 2, 2, 2])
predicted = np.array([1, 1, 2, 1, 1, 1, 2, 2, 1, 2])

我想创建 2 个新数组,分别包含被错误预测为其他内容的 1 的索引和错误预测为其他内容的 2 的索引。预期结果:

incorrect_ones = [2]
incorrect_twos = [5, 8]

必须有一些 NumPy 方法来实现这一点......有什么想法吗? 谢谢。

1 个答案:

答案 0 :(得分:0)

计算布尔条件并找到True值的位置索引:

np.where((correct == 1) & (predicted == 2))[0]
# array([2])
np.where((correct == 2) & (predicted == 1))[0]
# array([5, 8])