Numpy - 用 NaN 替换数字 [英] Numpy - Replace a number with NaN

查看:36
本文介绍了Numpy - 用 NaN 替换数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 numpy 中的 NaN 替换一个数字,并且正在寻找一个类似于 numpy.nan_to_num 的函数,但反向除外.

I am looking to replace a number with NaN in numpy and am looking for a function like numpy.nan_to_num, except in reverse.

随着处理不同的数组,该数字可能会发生变化,因为每个数组都可以有一个唯一定义的 NoDataValue.我看到有人使用字典,但数组很大并且充满了正负浮点数.我怀疑尝试将所有这些加载到任何东西中以创建密钥效率不高.

The number is likely to change as different arrays are processed because each can have a uniquely define NoDataValue. I have see people using dictionaries, but the arrays are large and filled with both positive and negative floats. I suspect that it is not efficient to try to load all of these into anything to create keys.

我尝试使用以下和 numpy 要求我使用 any() 或 all().我意识到我需要明智地迭代元素,但希望内置函数可以实现这一点.

I tried using the following and numpy requiring that I use any() or all(). I realize that I need to iterate element wise, but hope that a built-in function can achieve this.

def replaceNoData(scanBlock, NDV):
    for n, i in enumerate(array):
        if i == NDV:
            scanBlock[n] = numpy.nan

NDV 是 GDAL 的无数据值,数组是一个 numpy 数组.

NDV is GDAL's no data value and array is a numpy array.

掩码数组是否可行?

推荐答案

A[A==NDV]=numpy.nan

A==NDV 将产生一个布尔数组,可用作 A 的索引

A==NDV will produce a boolean array that can be used as an index for A

这篇关于Numpy - 用 NaN 替换数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆