这是一种方法-
def max_repeatedNaNs(a): # Mask of NaNs mask = np.concatenate(([False],np.isnan(a),[False])) if ~mask.any(): return 0 else: # Count of NaNs in each NaN group. Then, get max count as o/p. c = np.flatnonzero(mask[1:] < mask[:-1]) - np.flatnonzero(mask[1:] > mask[:-1]) return c.max()
这是一个改进的版本-
def max_repeatedNaNs_v2(a): mask = np.concatenate(([False],np.isnan(a),[False])) if ~mask.any(): return 0 else: idx = np.nonzero(mask[1:] != mask[:-1])[0] return (idx[1::2] - idx[::2]).max()
基准测试
@pltrdy's comment-
In [77]: a = np.random.rand(10000)In [78]: a[np.random.choice(range(len(a)),size=1000,replace=0)] = np.nanIn [79]: %timeit contiguous_NaN(a) #@pltrdy's solution100 loops, best of 3: 15.8 ms per loopIn [80]: %timeit max_repeatedNaNs(a)10000 loops, best of 3: 103 µs per loopIn [81]: %timeit max_repeatedNaNs_v2(a)10000 loops, best of 3: 86.4 µs per loop
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)