简体   繁体   English

列表中点之间的线性插值

[英]Linear interpolation between points in a list

I have a list in python like this [4, 0, 0, 6, 0, 8, 0, 0, 0, 3] , and I want to convert into something like this [4, 4.67, 5.33, 6, 7, 8, 6.75, 5.5, 4.25, 3] .我在 python 中有一个这样的列表[4, 0, 0, 6, 0, 8, 0, 0, 0, 3] ,我想转换成这样的[4, 4.67, 5.33, 6, 7, 8, 6.75, 5.5, 4.25, 3] Basically just replacing zeroes with an interpolation of the points already in the list.基本上只是用列表中已有的点的插值替换零。 Any ideas?有任何想法吗?

One way using pandas.Series.interpolate :使用pandas.Series.interpolate一种方法:

import pandas as pd

pd.Series([i  if i else np.nan for i in l]).interpolate().tolist()

Output:输出:

[4.0,
 4.666666666666667,
 5.333333333333333,
 6.0,
 7.0,
 8.0,
 6.75,
 5.5,
 4.25,
 3.0]

Using accumulate from itertools, you can find the starting and ending indexes for streaks of zeros around each position.使用 itertools 中的累积,您可以找到每个位置周围零条纹的开始和结束索引。 Then use these ranges to compute a linear ratio for each zero position relative to its starting and ending non-zero range boundaries:然后使用这些范围计算每个零位置相对于其起始和结束非零范围边界的线性比率:

from itertools import accumulate

n = [4, 0, 0, 6, 0, 8, 0, 0, 0, 3]
starts = accumulate(range(len(n)),lambda a,b: b if n[b] else a)
ends   = [*accumulate(reversed(range(len(n))),lambda a,b: b if n[b] else a)][::-1]
inter  = [ n[i] or n[s]+(n[e]-n[s])*(i-s)/(e-s) for i,(s,e) in enumerate(zip(starts,ends)) ]

# inter = [4, 4.666666666666667, 5.333333333333333, 6, 7.0, 8, 6.75, 5.5, 4.25, 3]

The starts list will contain the index of the previous non-zero value for each position (uses the position itself for non-zero values): starts列表将包含每个位置的前一个非零值的索引(将位置本身用于非零值):

[0, 0, 0, 3, 3, 5, 5, 5, 5, 9]

The ends list contains the index of the next non-zero values ends列表包含下一个非零值的索引

[0, 3, 3, 3, 5, 5, 9, 9, 9, 9]

Combining these two lists using zip we obtain all the information needed to compute the intermediate values:使用 zip 组合这两个列表,我们获得计算中间值所需的所有信息:

                         start  end    range  position        Interpolation 
index value start end    value  value  size   in range     ratio        value
(i)   n[i]  (s)   (e)    n[s]   n[e]   e-s    i-s       (i-s)/(e-s)   see below
 0     4     0     0      4      4      0     0            -----          4
 1     0     0     3      4      6      3     1            0.67          4.67
 2     0     0     3      4      6      3     2            0.33          5.33
 3     6     3     3      6      6      0     0            -----          6
 4     0     3     5      6      8      2     1            0.50          7.00
 5     8     5     5      8      8      0     0            -----          8
 6     0     5     9      8      3      4     1            0.75          6.75
 7     0     5     9      8      3      4     2            0.50          5.50
 8     0     5     9      8      3      4     3            0.25          4.25
 9     3     9     9      3      3      0     0            -----          3

Keeping non-zero values where present and calculating the interpolated value as startValue + (endValue-startValue) x InterpolationRatio for zero positions.保留存在的非零值,并将内插值计算为startValue + (endValue-startValue) x InterpolationRatio为零位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM