计算 NumPy 数组中非 NaN 元素的数量
在本文中,我们将了解如何在Python中计算 NumPy 数组中非 NaN 元素的数量。
NAN:当您不在乎该位置的值时使用它。也许有时用于代替丢失的数据或损坏的数据。
方法一:使用条件
在这个例子中,我们将使用一维数组。在下面给出的代码中,我们遍历给定 NumPy 数组的每个条目并检查该值是否为 NaN。
Python3
import numpy as np
ex1 = np.array([1, 4, -9, np.nan])
ex2 = np.array([1, 45, -2, np.nan, 3,
-np.nan, 3, np.nan])
def approach_1(data):
# here the input data, is a numpy ndarray
# initialize the number of non-NaN elements
# in data
count = 0
# loop over each entry of the data
for entry in data:
# check whether the entry is a non-NaN value
# or not
if not np.isnan(entry):
# if not NaN, increment "count" by 1
count += 1
return count
print(approach_1(ex1))
print(approach_1(ex2))
Python3
import numpy as np
ex3 = np.array([[3, 4, -390, np.nan],
[np.nan, np.nan, np.nan, -90]])
def approach_2(data):
return np.sum(~np.isnan(data))
print(approach_2(ex3))
Python3
import numpy as np
ex4 = np.array([[0.35834379, 0.67202438, np.nan, np.nan,
np.nan, 0.47870971],
[np.nan, np.nan, np.nan, 0.08113384,
0.70511741, 0.15260996],
[0.09028477, np.nan, 0.16639899,
0.47740582, 0.7259116, 0.94797347],
[0.80305651, np.nan, 0.67949724,
0.84112054, 0.15951702, 0.07510587],
[0.28643337, 0.00804256, 0.36775056,
0.19360266, 0.07288145, 0.37076932]])
def approach_3(data):
return data.size - np.count_nonzero(np.isnan(data))
print(approach_3(ex4))
输出:
3
5
方法二:使用 isnan()
使用 NumPy 数组的功能,我们可以一次对整个数组而不是单个元素执行操作。
使用的函数:
- np.isnan(data):对数组的条目、data 执行 np.isnan() 操作后返回一个布尔数组
- np.sum():由于我们向 sum函数输入一个布尔数组,它返回布尔数组中真值(1s)的数量。
蟒蛇3
import numpy as np
ex3 = np.array([[3, 4, -390, np.nan],
[np.nan, np.nan, np.nan, -90]])
def approach_2(data):
return np.sum(~np.isnan(data))
print(approach_2(ex3))
输出:
4
方法 3:使用np.count_nonzero()函数
numpy.count_nonzero()函数计算数组 arr 中非零值的数量。
Syntax : numpy.count_nonzero(arr, axis=None)
Parameters :
arr : [array_like] The array for which to count non-zeros.
axis : [int or tuple, optional] Axis or tuple of axes along which to count non-zeros. Default is None, meaning that non-zeros will be counted along a flattened version of arr.
Return : [int or array of int] Number of non-zero values in the array along a given axis. Otherwise, the total number of non-zero values in the array is returned.
蟒蛇3
import numpy as np
ex4 = np.array([[0.35834379, 0.67202438, np.nan, np.nan,
np.nan, 0.47870971],
[np.nan, np.nan, np.nan, 0.08113384,
0.70511741, 0.15260996],
[0.09028477, np.nan, 0.16639899,
0.47740582, 0.7259116, 0.94797347],
[0.80305651, np.nan, 0.67949724,
0.84112054, 0.15951702, 0.07510587],
[0.28643337, 0.00804256, 0.36775056,
0.19360266, 0.07288145, 0.37076932]])
def approach_3(data):
return data.size - np.count_nonzero(np.isnan(data))
print(approach_3(ex4))
输出:
22