Reputation: 857
I have to compare two numpy arrays regardless of their order. I had hoped that numpy.array_equiv(a, b) will do the trick but unfortunately, it doesn't. Example:
a = np.array([[3, 1], [1,2]])
b = np.array([[1, 2], [3, 1]])
print (np.array_equiv(a, b))`# return false
Any suggestions? Thanks in advance
Upvotes: 7
Views: 6088
Reputation: 19957
You can probably just do:
#np.sort can take an axis parameter. For your case we will sort row wise.
np.array_equiv(np.sort(a,axis=0), np.sort(b,axis=0))
Out[178]: True
Upvotes: 3
Reputation: 114976
You could use np.array_equal(np.sort(a.flat), np.sort(b.flat))
In [56]: a = np.array([[3, 1], [1, 2]])
In [57]: b = np.array([[1, 2], [3, 1]])
In [58]: np.array_equal(np.sort(a.flat), np.sort(b.flat))
Out[58]: True
In [59]: b = np.array([[1, 2], [3, 4]])
In [60]: np.array_equal(np.sort(a.flat), np.sort(b.flat))
Out[60]: False
In [61]: b = np.array([[1, 2], [3, 3]])
In [62]: np.array_equal(np.sort(a.flat), np.sort(b.flat))
Out[62]: False
Upvotes: 3