gpt4 book ai didi

python - 从数据点不按时间匹配的时间序列图中求和/堆叠值的算法

转载 作者:太空宇宙 更新时间:2023-11-03 11:10:39 24 4
gpt4 key购买 nike

我有一个图形/分析问题,我无法完全理解。我可以做一个蛮力,但它太慢了,也许有人有更好的主意,或者知道或快速的 python 库?

我有 2 个以上的时间序列数据集 (x,y),我想聚合(并随后绘制)。问题是系列中的 x 值不匹配,我真的不想求助于将值复制到时间段中。

所以,给定这两个系列:

S1: (1;100) (5;100) (10;100)
S2: (4;150) (5;100) (18;150)

加起来应该是:

ST: (1;100) (4;250) (5;200) (10;200) (18;250)

逻辑:

x=1 s1=100, s2=None, sum=100
x=4 s1=100, s2=150, sum=250 (note s1 value from previous value)
x=5 s1=100, s2=100, sum=200
x=10 s1=100, s2=100, sum=200
x=18 s1=100, s2=150, sum=250

我目前的想法是迭代一个排序的键列表(x),为每个系列保留以前的值,并查询每个集合是否有一个新的 y 作为 x。

如有任何想法,我们将不胜感激!

最佳答案

这是另一种方法,将更多行为放在各个数据流上:

class DataStream(object):
def __init__(self, iterable):
self.iterable = iter(iterable)
self.next_item = (None, 0)
self.next_x = None
self.current_y = 0
self.next()

def next(self):
if self.next_item is None:
raise StopIteration()
self.current_y = self.next_item[1]
try:
self.next_item = self.iterable.next()
self.next_x = self.next_item[0]
except StopIteration:
self.next_item = None
self.next_x = None
return self.next_item

def __iter__(self):
return self


class MergedDataStream(object):
def __init__(self, *iterables):
self.streams = [DataStream(i) for i in iterables]
self.outseq = []

def next(self):
xs = [stream.next_x for stream in self.streams if stream.next_x is not None]
if not xs:
raise StopIteration()
next_x = min(xs)
current_y = 0
for stream in self.streams:
if stream.next_x == next_x:
stream.next()
current_y += stream.current_y
self.outseq.append((next_x, current_y))
return self.outseq[-1]

def __iter__(self):
return self


if __name__ == '__main__':
seqs = [
[(1, 100), (5, 100), (10, 100)],
[(4, 150), (5, 100), (18, 150)],
]

sm = MergedDataStream(*seqs)
for x, y in sm:
print "%02s: %s" % (x, y)

print sm.outseq

关于python - 从数据点不按时间匹配的时间序列图中求和/堆叠值的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4497038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com