我必须比较两个numpy数组,无论它们的顺序如何。我曾希望numpy.array_equiv(a,b)能够做到这一点但不幸的是,它没有。例如:
a = np.array([[3, 1], [1,2]])
b = np.array([[1, 2], [3, 1]])
print (np.array_equiv(a, b))`# return false
有什么建议吗?提前致谢
答案 0 :(得分:1)
您可以这样做:
#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
答案 1 :(得分:0)
您可以使用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