计算NumPy数组中非NaN元素的数量 | 您所在的位置:网站首页 › 求数组个数的函数 › 计算NumPy数组中非NaN元素的数量 |
计算NumPy数组中非NaN元素的数量
在这篇文章中,我们将看到如何用Python计算NumPy数组中非NaN元素的数量。 NAN:当你不关心该位置的值是什么时,就会使用它。也许有时会被用来代替缺失的数据,或损坏的数据。 方法1:使用条件在这个例子中,我们将使用一维数组。在下面给出的代码中,我们在给定的NumPy数组的每个条目上循环,并检查该值是否为NaN。 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))输出: 3 5 方法2:使用isan()利用NumPy数组的功能,我们可以一次对整个数组进行操作,而不是单个元素。 使用到的函数: np.isan(data)。对数组中的条目data进行np.isan()操作后返回一个布尔数组。 np.sum()。由于我们向sum函数输入的是一个布尔数组,所以它返回布尔数组中真值(1)的数量。 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中非零值的数量。 语法: numpy.count_nonzero(arr, axis=None) 参数 : arr : [array_like] 用于计算非零的数组。 axis : [int or tuple, optional] 用于计算非零点的轴或轴的元组。默认为无,意味着非零点将沿着Arr的一个扁平化版本进行计数。 返回 : [int or array of int] 数组中沿给定轴的非零值的数量。否则,将返回数组中非零值的总数。 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 |
CopyRight 2018-2019 实验室设备网 版权所有 |