Reputation: 159
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
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 False
s and True
s 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 int
s before feeding into numpy.flatnonzero
.
Upvotes: 4
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