目录
使用NumPy创建一个数组,它是给定大小的每个连续子数组的平均值
计算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