Python 一维numpy阵列中间隙大小的识别

Python 一维numpy阵列中间隙大小的识别,python,functional-programming,interpolation,Python,Functional Programming,Interpolation,我有一个numpy数组,其中包含各种大小的间隙。我想用线性插值来填补size

我有一个numpy数组,其中包含各种大小的间隙。我想用线性插值来填补size 换言之:

N = 2

我想用30.0填充第三个(索引2)条目

我对算法方法持开放态度,但我的意图是创建一个数组,作为局部差距大小的指示器:

[0 0 1 0 0 3 3 3 0 0]  
或者差距太大:

[0 0 0 0 0 1 1 1 0 0]
有了它,我可以记录足够小的差距指数,并使用interp1d。有没有一种经济、实用的方法可以做到这一点?我知道如何使用前进标记前进标记循环

谢谢


Eli

我不确定这是否正是您想要的,但这是我的建议:

>>> import numpy as np
>>> from itertools import groupby
>>>
>>> x = np.array([10., 20., np.nan, 40., 50., np.nan, np.nan, np.nan, 10.,0.,-10.])
>>> y = np.zeros_like(x, dtype=int)
>>> y[np.where(np.isnan(x))] = 1 # Locate where the array is nan
>>> y
array([0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0])
>>> z = []
>>> for a, b in groupby(y, lambda x: x == 0):
...     if a: # Where the value is 0, simply append to the list
...         z.extend(list(b))
...     else: # Where the value is one, replace 1 with the number of sequential 1's
...         l = len(list(b))
...         z.extend([l]*l)
>>> z
[0, 0, 1, 0, 0, 3, 3, 3, 0, 0, 0]

因此,在第一个数组中(
[0 0 1 0 0 3 3 0 0]
1
3
指示没有“正确”编号的连续元素的数量?确切地说,SethMMorton。
>>> import numpy as np
>>> from itertools import groupby
>>>
>>> x = np.array([10., 20., np.nan, 40., 50., np.nan, np.nan, np.nan, 10.,0.,-10.])
>>> y = np.zeros_like(x, dtype=int)
>>> y[np.where(np.isnan(x))] = 1 # Locate where the array is nan
>>> y
array([0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0])
>>> z = []
>>> for a, b in groupby(y, lambda x: x == 0):
...     if a: # Where the value is 0, simply append to the list
...         z.extend(list(b))
...     else: # Where the value is one, replace 1 with the number of sequential 1's
...         l = len(list(b))
...         z.extend([l]*l)
>>> z
[0, 0, 1, 0, 0, 3, 3, 3, 0, 0, 0]