问题描述
我正在尝试填写不完整的数字列表,我找不到任何Python方式来做到这一点.我有一个从1到31的天序列,每一天都有一个浮点值.
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:
样本月份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()循环很迷路...也许数学库中有一个方法,但我找不到它.
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.
感谢您的帮助!
我想对数字进行插值,但据我所知,只有numpy/scipy具有此类数学函数,而即时通讯使用的是与numpy/scipy不兼容的Pypy.
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.
推荐答案
您只需要一些简单的循环和良好的旧编程逻辑即可.此逻辑中的一个警告是,您需要一个开始和结束编号才能使其正常工作.我不知道这对您的数据是否有意义,但是插值法要求这样做.
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.
设置:
# 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
这篇关于内插数字序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!