In numpy is it possible to make a difference between this 2 arrays:
[[0 0 0 0 1 1 1 1 2 2 2 2]
[0 1 2 3 0 1 2 3 0 1 2 3]]
[[0 0 0 0 1 1 1 2 2 2]
[0 1 2 3 0 2 3 0 1 2]]
to have this result
[[1 2]
[1 3]]
?
In numpy is it possible to make a difference between this 2 arrays:
[[0 0 0 0 1 1 1 1 2 2 2 2]
[0 1 2 3 0 1 2 3 0 1 2 3]]
[[0 0 0 0 1 1 1 2 2 2]
[0 1 2 3 0 2 3 0 1 2]]
to have this result
[[1 2]
[1 3]]
?
This is one way. You can also use numpy.unique
for a similar solution (easier in v1.13+, see Find unique rows in numpy.array), but if performance is not an issue you can use set
.
import numpy as np
A = np.array([[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2],
[0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]])
B = np.array([[0, 0, 0, 0, 1, 1, 1, 2, 2, 2],
[0, 1, 2, 3, 0, 2, 3, 0, 1, 2]])
res = np.array(list(set(map(tuple, A.T)) - set(map(tuple, B.T)))).T
array([[2, 1],
[3, 1]])
We can think 2D array as 2 times of 1D array and using numpy.setdiff1d to compare them.
What about:
a=[[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]]
b=[[0, 0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 3, 0, 2, 3, 0, 1, 2]]
a = np.array(a).T
b = np.array(b).T
A = [tuple(t) for t in a]
B = [tuple(t) for t in b]
set(A)-set(B)
Out: {(1, 1), (2, 3)}