如何将无数据值 -3.4028231e+38 替换为 numpy.nan

How to replace no data value -3.4028231e+38 to numpy.nan

我有一个由光栅图像构建的二维数组。光栅图像没有分配给 -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

输出是'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()

它是否必须等于 -3.40282306e+38 才能让您的算法正常工作? 如果也不尝试不平等而不是平等。如果浮点数是完美的整数(例如 0),则等式适用于浮点数,对于像您这样的数字,使用不等式可能就是答案。不会像

(-3.40282306e+38-smallnumber) fix your problem?-You have to set small number to somthing in the context of your problem which I dont know what it is.

考虑到您知道您的实际值介于 -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