interpolate number sequence

蹲街弑〆低调 提交于 2019-12-06 13:55:49

You just need some simple looping and good old programming logic. The one caveat in this logic is that you need a start and end number in order for it to work. I don't know if that makes sense for your data, but interpolation requires that.

Setup:

# 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

Processing:

# 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

Consider using pandas for this, the interpolate method makes it easy:

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

And with multiple missing values:

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

And you can convert it back to a dict if you need to:

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

I think use the scipy's interpolate methods is a smart way

first turn your data to an easy to manipulate format:

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]

then create the linear interpolate function and output the middle value

# 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))

Result:

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!