[英]How can I compare two 2D NumPy arrays of size 40k × 40k?
I must compare 2 NumPy arrays of floats.我必须比较浮动的 2 个 NumPy arrays。 Unfortunately of the order of 10^-3 or smaller, of size 40k × 40k.
不幸的是,大小为 10^-3 或更小,大小为 40k × 40k。 They must be identical.
它们必须相同。 Can I directly print/mask the elements that differ?
我可以直接打印/屏蔽不同的元素吗?
I tried allclose
and isclose
:我尝试
allclose
和isclose
:
>>> import numpy as np
>>> F_sp = np.load('./Matrix/Fmatrix.npy')
>>> F_org = np.load('./../Org/FMatrix.npy')
>>> print(" all close? {} ".format(np.allclose(F_sp,F_org,equal_nan=True)))
all close? False
>>> print(" is close? {} ".format(np.isclose(F_sp,F_org,equal_nan=True)))
is close? [[ True True True ... True True True]
[ True True True ... True True True]
[ True True True ... True True True]
...
[ True True True ... True True True]
[ True True True ... True True True]
[ True True True ... True True True]]
>>> print(" is close? {} ".format(np.isclose(F_sp,F_org,equal_nan=False)))
is close? [[ True True True ... True True True]
[ True True True ... True True True]
[ True True True ... True True True]
...
[ True True True ... True True True]
[ True True True ... True True True]
[ True True True ... True True True]]
>>> np.set_printoptions(threshold=sys.maxsize)
>>> print(" is close? {} ".format(np.isclose(F_sp,F_org,equal_nan=True)))
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
print(" is close? {} ".format(np.isclose(F_sp,F_org,equal_nan=True)))
OverflowError: cannot serialize a string larger than 4GiB
If they must be identical, it is not necessary to check if the values are close, but simply if they are identical.如果它们必须相同,则不必检查值是否接近,而只需检查它们是否相同。
Using:使用:
identical = F_sp == F_org
should give an array with true and false.应该给出一个真假数组。 To print the elements that differ, use
np.where(condition)
.要打印不同的元素,请使用
np.where(condition)
。 This will give an array with the elements that fill the condition.这将给出一个包含满足条件的元素的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.