简体   繁体   English

如何比较大小为 40k × 40k 的两个 2D NumPy arrays?

[英]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 :我尝试allcloseisclose

>>> 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.

相关问题 对文件夹中的 40k 图像进行排序 python - Sort 40k images in a folder python 当文件大小大于40K字节时,为什么csv.reader失败? - Why does csv.reader fail when the file size is larger than 40K bytes? Python Scrapy 在 40k 请求后卡住 - Python Scrapy gets stuck after 40k requests DBSCAN 集群甚至无法处理 40k 数据,但使用 python 和 sklearn 处理 10k 数据 - DBSCAN clustering is not working even on 40k data but working on 10k data using python and sklearn 如何在 Python 中插入未排序的 2D numpy 数组并将插入的值与原始值进行比较? - How can I interpolate unsorted 2D numpy arrays in Python and compare the interpolated values to the original one? Python,而不是一个包含4000万个连续数字的列表,我如何制作40个100万个连续数字列表的2-d列表? - Python, instead of a list of 40 million consecutive numbers how would I make a 2-d list of 40, size 1 million, consecutive number lists? 如何连接2D numpy数组列表? - How can I concatenate a list of 2D numpy arrays? numpy:如何根据一维数组中的条件从两个2D数组中选择行? - numpy: How do I pick rows from two 2D arrays based on conditions in 1D arrays? 如何将2d numpy数组列表连接到3d numpy数组? - How can I concatenate a list of 2d numpy arrays into a 3d numpy array? 如何将许多 2D numpy 数组快速放入 4D numpy 数组中? - How can I put many 2D numpy arrays fast in a 4D numpy array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM