我有一个由光栅图像构造的二维数组。光栅图像没有分配给-3.4028231e+38的数据值,我试图用“nan”替换该值,但当我对其应用条件运算符时,我无法找到该值。
我的数据如下:
>>> slice22 = inndvi[0:2,0:2]
>>> slice22
array([[ -3.40282306e+38, -3.40282306e+38],
[ -3.40282306e+38, -3.40282306e+38]], dtype=float32)
当我尝试在 if 语句中检查这些值时:
>>> if slice22[0][0] ==-3.40282306e+38:
... print "yes"
... else:
... print "no"
...
no
输出为“否”
因此,我无法将 3.40282306e+38 分配给 numpy.nan,如下所示:
slice22[slice22 == 3.40282306e+38] = numpy.nan
我还想提一下,我的数据集在栅格中的范围是从 +2 到 -2。 我尝试使用该范围来消除 3.40282306e+38 值,但仍然出现错误。
>>> slice22 [slice22 < 2 and slice22 >2 ]= np.nan
Runtime error
Traceback (most recent call last):
File "<string>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
最佳答案
考虑到您知道实际值在 -2 到 2 之间,您可以轻松过滤掉该范围之外的任何内容。
a[(a < -2) | (a > 2)] = np.nan #option 1
a[np.abs(a) > 2] = np.nan #option 2
a[np.logical_or(a < -2, a > 2)] = np.nan #option 3
关于python - 如何将无数据值-3.4028231e+38替换为numpy.nan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35030052/