Alex K
Alex K

Reputation: 159

Determining the indicies where 2 numpy arrays differ

I've got 2 long numpy arrays, both having 508 elements. I would like to get the indices of where they differ. Most of the solutions I've seen determining difference here sort the arrays which is not suitable in my case.

Example

arr_1 = [0, 1, 4, 0, 2, 2, 0, 3, 5, ... , 5, 5, 6]

arr_1 = [0, 1, 0, 0, 0, 2, 0, 3, 0, ... , 5, 0, 6]

Hopefully returning something like:

solut = [0, 0, 1, 0, 1, 0, 0, 0, 1, ... , 0, 1, 0]

or even

solut = [2, 4, 8, ..., n-2]

Is there an efficient way to do this in numpy?

Upvotes: 2

Views: 1558

Answers (2)

Daweo
Daweo

Reputation: 36520

I would harness numpy.equal following way

import numpy as np 
arr_1 = [0, 1, 4, 0, 2, 2, 0, 3, 5, 5, 5, 6]
arr_2 = [0, 1, 0, 0, 0, 2, 0, 3, 0, 5, 0, 6]
solut = (~np.equal(arr_1, arr_2)).astype(int)
print(solut)

output

[0 0 1 0 1 0 0 0 1 0 1 0]

Explanation: use numpy.equal to get array of Falses and Trues where arr_1-arr_2 pairs are equal, then negating (using ~) finally converting booleans to int. If you are interested in indices of nonzero use numpy.flatnonzero function:

indices = np.flatnonzero(solut)
print(indices)

output

[ 2  4  8 10]

In this case you do not need to convert array to ints before feeding into numpy.flatnonzero.

Upvotes: 4

Bananenkönig
Bananenkönig

Reputation: 543

You can just write arr = arr_1 == arr_2. This code gives you a boolean array with true if they are equal and false if not. Then you can use np.where to find the indices where the arrays are equal or not.

Upvotes: 2

Related Questions