[英]interpolate number sequence
I am trying to complete an uncomplete list of numbers, I couldn't find any pythonic way to do it. 我正在尝试填写不完整的数字列表,找不到任何Python方式来实现。 I have a sequence of days from 1 to 31, and for each day, I have a float value.
我有一系列从1到31的天,并且每天都有一个float值。
#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: 样本月份1:
{16: 2.00, 18: 4.00}
#==> I want to add to the dictionnary 17: 3.00
sample month2: 样本月份2:
{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. 听起来很简单,但是我有数百万行要从一个不完整的sql数据库中处理,并且目前我对xrange()循环迷失了……也许在数学库中有一个方法,但我找不到它。
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. 编辑:我想对数字进行插值,但据我所知,只有numpy / scipy具有此类数学函数,并且im使用与numpy / scipy不兼容的Pypy。
Consider using pandas
for this, the interpolate
method makes it easy: 考虑为此使用
pandas
, interpolate
方法很容易:
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}
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
I think use the scipy's interpolate methods is a smart way 我认为使用scipy的插值方法是一种聪明的方法
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.