gpt4 book ai didi

python - pandas窗口函数: accessing to intermediate results

转载 作者:行者123 更新时间:2023-12-01 05:51:55 24 4
gpt4 key购买 nike

我正在做一些日志分析并每隔几分钟检查一次队列的长度。我知道文件何时进入“队列”(一个简单的文件系统目录)以及何时离开。这样,我可以绘制给定时间间隔的队列长度。到目前为止一切顺利,尽管代码有点程序化:

ts = pd.date_range(start='2012-12-05 10:15:00', end='2012-12-05 15:45', freq='5t')
tmpdf = df.copy()
for d in ts:
tmpdf[d] = (tmpdf.date_in < d)&(tmpdf.date_out > d)
queue_length = tmpdf[list(ts)].apply(func=np.sum)

但是,我想将实际长度与给定消耗率(例如每秒 1 个等)下的长度进行比较。我不能只减去一个常量,因为队列不能超过零。

我已经做到了,但是是以非常程序化的方式。我尝试使用 pandas 窗口函数但收效甚微,因为无法访问已为前一个元素计算的结果。这是我尝试的第一件事,但这是致命的错误:

imagenes_min = 60 * imagenes_sec
def roll(window_vals):
return max(0.0, window_vals[-1] + window_vals[-2] - imagenes_min)

pd.rolling_apply(arg=imagenes_introducidas, func=roll , window = 2, min_periods=2)

真正的代码是这样的,我认为它太冗长和缓慢:

imagenes_sec = 1.05
imagenes_min = imagenes_sec * 60 *5
imagenes_introducidas = df3.aet.resample(rule='5t',how='count')
imagenes_introducidas.head()

def accum_minus(serie, rate):
acc = 0
retval = np.zeros(len(serie))
for i,a in enumerate(serie.values):
acc = max(0, a + acc - rate)
retval[i] = acc
return Series(data=retval, index=serie.index)

est_1 = accum_minus(imagenes_introducidas, imagenes_min)
comparativa = DataFrame(data = { 'real': queue_length, 'est_1_sec': est_1 })
comparativa.plot()

compar

这似乎是一项简单的任务,但我不知道如何正确完成。也许 pandas 不是工具,而是一些 numpy 或 scipy 魔法。

更新:df3是这样的(省略了一些列):

                               aet             date_out
date_in
2012-12-05 10:08:59.318600 Z2XG17 2012-12-05 10:09:37.172300
2012-12-05 10:08:59.451300 Z2XG17 2012-12-05 10:09:38.048800
2012-12-05 10:08:59.587400 Z2XG17 2012-12-05 10:09:39.044100

更新2:这看起来更快,但仍然不太优雅

imagenes_sec = 1.05
imagenes_min = imagenes_sec * 60 *5
imagenes_introducidas = df3.aet.resample(rule='5t',how='count')

def add_or_zero(x, y):
return max(0.0, x + y - imagenes_min)

v_add_or_zero = np.frompyfunc(add_or_zero, 2,1)
xx = v_add_or_zero.accumulate(imagenes_introducidas.values, dtype=np.object)

dd = DataFrame(data = {'est_1_sec' : xx, 'real': queue_length}, index=imagenes_introducidas.index)
dd.plot()

最佳答案

将入站和出站事件交错到单个帧中怎么样?

In [15]: df
Out[15]:
date_in aet date_out
0 2012-12-05 10:08:59.318600 Z2XG17 2012-12-05 10:09:37.172300
1 2012-12-05 10:08:59.451300 Z2XG17 2012-12-05 10:09:38.048800
2 2012-12-05 10:08:59.587400 Z2XG17 2012-12-05 10:09:39.044100

In [16]: inbnd = pd.DataFrame({'event': 1}, index=df.date_in)

In [17]: outbnd = pd.DataFrame({'event': -1}, index=df.date_out)

In [18]: real_stream = pd.concat([inbnd, outbnd]).sort()

In [19]: real_stream
Out[19]:
event
date
2012-12-05 10:08:59.318600 1
2012-12-05 10:08:59.451300 1
2012-12-05 10:08:59.587400 1
2012-12-05 10:09:37.172300 -1
2012-12-05 10:09:38.048800 -1
2012-12-05 10:09:39.044100 -1

在此格式中(每次增量减一),队列深度可以使用 cumsum() 轻松计算。

In [20]: real_stream['depth'] = real_stream.event.cumsum()

In [21]: real_stream
Out[21]:
event depth
date
2012-12-05 10:08:59.318600 1 1
2012-12-05 10:08:59.451300 1 2
2012-12-05 10:08:59.587400 1 3
2012-12-05 10:09:37.172300 -1 2
2012-12-05 10:09:38.048800 -1 1
2012-12-05 10:09:39.044100 -1 0

要模拟不同的消费率,请替换所有真实的出站时间戳具有一系列以固定频率制造的出站时间戳。由于 cumsum() 函数在这种情况下不起作用,因此我创建了一个计数函数这需要一个下限值。

In [53]: outbnd_1s = pd.DataFrame({'event': -1},
....: index=real_stream.event.resample("S").index)

In [54]: fixed_stream = pd.concat([inbnd, outbnd_1s]).sort()

In [55]: def make_floor_counter(floor):
....: count = [0]
....: def process(n):
....: count[0] += n
....: if count[0] < floor
....: count[0] = floor
....: return count[0]
....: return process
....:

In [56]: fixed_stream['depth'] = fixed_stream.event.map(make_floor_counter(0))

In [57]: fixed_stream.head(8)
Out[57]:
event depth
2012-12-05 10:08:59 -1 0
2012-12-05 10:08:59.318600 1 1
2012-12-05 10:08:59.451300 1 2
2012-12-05 10:08:59.587400 1 3
2012-12-05 10:09:00 -1 2
2012-12-05 10:09:01 -1 1
2012-12-05 10:09:02 -1 0
2012-12-05 10:09:03 -1 0

关于python - pandas窗口函数: accessing to intermediate results,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13956564/

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