将NaN值转换为零

132 投票
9 回答
407725 浏览
提问于 2025-04-16 12:34

我有一个二维的numpy数组,这个数组里有些值是NaN(表示“不是一个数字”,通常用来表示缺失值)。我想用这个数组做一些操作。比如说,考虑这个数组:

[[   0.   43.   67.    0.   38.]
 [ 100.   86.   96.  100.   94.]
 [  76.   79.   83.   89.   56.]
 [  88.   NaN   67.   89.   81.]
 [  94.   79.   67.   89.   69.]
 [  88.   79.   58.   72.   63.]
 [  76.   79.   71.   67.   56.]
 [  71.   71.   NaN   56.  100.]]

我想逐行处理这个数组,把每一行的数从大到小排序,然后取出最大的三个值,最后算它们的平均值。我尝试的代码是:

# nparr is a 2D numpy array
for entry in nparr:
    sortedentry = sorted(entry, reverse=True)
    highest_3_values = sortedentry[:3]
    avg_highest_3 = float(sum(highest_3_values)) / 3

但是,这段代码在处理包含NaN的行时就不管用了。我的问题是,有没有简单的方法可以把这个二维numpy数组里的所有NaN值都换成零,这样我在排序和其他操作时就不会遇到问题了。

9 个回答

51

那你觉得 nan_to_num() 这个函数怎么样呢?

200

这里的 A 是你的二维数组:

import numpy as np
A[np.isnan(A)] = 0

这个 isnan 函数会生成一个布尔数组,告诉你哪里有 NaN 值。布尔数组就像一个面具,可以用来筛选出形状相同的数组中的元素。

149

这个应该可以工作:

from numpy import *

a = array([[1, 2, 3], [0, 3, NaN]])
where_are_NaNs = isnan(a)
a[where_are_NaNs] = 0

在上面的例子中,where_are_NaNs 是:

In [12]: where_are_NaNs
Out[12]: 
array([[False, False, False],
       [False, False,  True]], dtype=bool)

关于效率的小补充。下面的例子是在 numpy 1.21.2 版本下运行的。

>>> aa = np.random.random(1_000_000)
>>> a = np.where(aa < 0.15, np.nan, aa)
>>> %timeit a[np.isnan(a)] = 0
536 µs ± 8.11 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
>>> a = np.where(aa < 0.15, np.nan, aa)
>>> %timeit np.where(np.isnan(a), 0, a)
2.38 ms ± 27.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
>>> a = np.where(aa < 0.15, np.nan, aa)
>>> %timeit np.nan_to_num(a, copy=True)
8.11 ms ± 401 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
>>> a = np.where(aa < 0.15, np.nan, aa)
>>> %timeit np.nan_to_num(a, copy=False)
3.8 ms ± 70.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

因此,a[np.isnan(a)] = 0 的速度更快。

撰写回答