user906780
user906780

Reputation:

interpolate number sequence

I am trying to complete an uncomplete list of numbers, I couldn't find any pythonic way to do it. I have a sequence of days from 1 to 31, and for each day, I have a float value.

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

but my data is uncomplete, and some days are missing! therefore I want to fill the missing picture mathematically this way:

sample month1:

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

sample month2:

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

sounds simple but I have litteraly millions of rows to treat from an uncomplete sql database and for the moment I am quite lost in for xrange() loops... Maybe there is a method in the math lib but I couldn't find it.

thanks for your help!

EDIT: I want to interpolate the numbers, but as far as I know, only numpy/scipy have these kind of math functions, and im using Pypy which is not compatible with numpy/scipy.

Upvotes: 2

Views: 2637

Answers (3)

StanleyZ
StanleyZ

Reputation: 607

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

Upvotes: 0

root
root

Reputation: 80346

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}

Upvotes: 5

gahooa
gahooa

Reputation: 137262

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

Upvotes: 1

Related Questions