Comparing a NumPy array to another

Viewed 26

I have 2 NumPy arrays, such as:

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

I would like to create 2 new arrays, which contain the indices of 1's incorrectly predicted as something else and the indices of 2's incorrectly predicted as something else, respectively. Desired result:

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

There just has to be some NumPy way to achieve this... Any ideas? Thanks.

1 Answers

Calculate the boolean conditions and find the indexes of the locations of the True values:

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