内插数字序列

时间:2012-10-19 21:36:44

标签: python math pypy

我正在尝试完成一个不完整的数字列表,我找不到任何pythonic方法来做到这一点。 我有一个从1到31天的序列,并且对于每一天,我都有一个浮动值。

#dictionnary{day: value}
monthvalues = {1: 1.12, 2: 3.24, 3: 2.23, 5: 2.10, 7: 4.97} etc.. to 31st day

但我的数据不完整,有些日子不见了!因此,我想以这种方式用数学方式填补缺失的图片:

样本月1:

{16: 2.00, 18: 4.00}
#==> I want to add to the dictionnary 17: 3.00

样本月2:

{10: 2.00, 14: 4.00}
#==> I want to add to the dictionnary 11: 2.25, 12: 2.50, 13: 2.75

听起来很简单,但我已经完成了数百万行来处理不完整的sql数据库,目前我已经完全迷失了xrange()循环...... 也许数学库中有一个方法,但我找不到它。

感谢您的帮助!

修改 我想插入数字,但据我所知,只有numpy / scipy有这些数学函数,而且我使用的Pypy与numpy / scipy不兼容。

3 个答案:

答案 0 :(得分:5)

考虑使用pandasinterpolate方法可以轻松实现:

In [502]: import pandas    

In [503]: s = pandas.Series({1: 1.12, 2: 3.24, 3: 2.23,5: 2.10,7:4.97}, index=range(1,8))

In [504]: s
Out[504]: 
1    1.12
2    3.24
3    2.23
4     NaN
5    2.10
6     NaN
7    4.97

In [505]: s.interpolate()
Out[505]: 
1    1.120
2    3.240
3    2.230
4    2.165
5    2.100
6    3.535
7    4.970

并且有多个缺失值:

In [506]: s2 = pandas.Series({10: 2.00, 14: 4.00},index=range(10,15))

In [507]: s2
Out[507]: 
10     2
11   NaN
12   NaN
13   NaN
14     4

In [508]: s2.interpolate()
Out[508]: 
10    2.0
11    2.5
12    3.0
13    3.5
14    4.0

如果你需要,你可以将它转换回dict:

In [511]: s2.to_dict()
Out[511]: {10: 2.0, 11: 2.5, 12: 3.0, 13: 3.5, 14: 4.0}

答案 1 :(得分:1)

你只需要一些简单的循环和良好的旧编程逻辑。这个逻辑中的一个警告是,您需要一个开始和结束编号才能使其工作。我不知道这对你的数据是否有意义,但插值需要这个。

设定:

# Keeps track of the last "seen" day
lastday=0

# Default 1st day if missing
if 1 not in monthvalues:
  monthvalues[1] = 1.23 #you need a default

# Default 31st day if missing
if 31 not in monthvalues:
  monthvalues[31] = 1.23 #you need a default

处理:

# Loop from 1 to 31
for thisday in range(1,32):

  # If we do not encounter thisday in the monthvalues, then skip and keep looping
  if thisday not in monthvalues:
    continue

  # How far ago was the last day seen?
  gap = thisday - lastday

  # If the last day was more than 1 ago, it means there is at least one day amis
  if gap > 1:

    # This is the amount of the last "seen" day
    last_amt = monthvalues[lastday]

    # this is the difference between the current day and the last day
    diff = monthvalues[thisday] - last_amt

    # This is how much you want to interpolate per day in-between
    amt_per_day = diff/gap

    # there is a gap of missing days, let's fill them
    # Start at 1 because we start at the day after the last seen day
    for n in range(1, gap):

      # Fill the missing days with an interpolated value
      monthvalues[lastday+n] = last_amt + amt_per_day * n

  # For the next iteration of the loop, this is the last seen day.
  lastday = thisday

答案 2 :(得分:0)

我认为使用scipy's interpolate methods是一种聪明的方法

首先将数据转换为易于操作的格式:

monthvalue = {1: 1.12, 2: 3.24, 3: 2.23, 5: 2.10, 7: 4.97, 6: 3.10, 10: 3.3}
X = sorted(monthvalue.keys())
Y = [monthvalue[x] for x in X]

然后创建线性插值函数并输出中间值

# interpolate function
f = interp1d(X, Y, kind='linear')

x_new = range(X[0], X[-1]+1, 1)
for x in x_new:
    print "%s: %s" % (x, f(x))

结果:

1: 1.12
2: 3.24
3: 2.23
4: 2.165
5: 2.1
6: 3.1
7: 4.97
8: 4.41333333333
9: 3.85666666667
10: 3.3