gpt4 book ai didi

python - 滚动窗口的数据帧表示

转载 作者:太空狗 更新时间:2023-10-29 21:28:24 35 4
gpt4 key购买 nike

我想要滚动窗口的数据框表示。我不需要在滚动窗口上执行某些操作,而是想要一个数据框,其中窗口以另一个维度表示。这可以是 pd.Panelnp.array 或带有 pd.MultiIndexpd.DataFrame .

设置

import pandas as pd
import numpy as np

np.random.seed([3,1415])
df = pd.DataFrame(np.random.rand(10, 3).round(2),
columns=['A', 'B', 'C'],
index=list('abcdefghij'))

print df

A B C
a 0.44 0.41 0.46
b 0.47 0.46 0.02
c 0.85 0.82 0.78
d 0.76 0.93 0.83
e 0.88 0.93 0.72
f 0.12 0.15 0.20
g 0.44 0.10 0.28
h 0.61 0.09 0.84
i 0.74 0.87 0.69
j 0.38 0.23 0.44

预期输出

对于 window = 2,我希望结果是这样。

      0                 1            
A B C A B C
a 0.44 0.41 0.46 0.47 0.46 0.02
b 0.47 0.46 0.02 0.85 0.82 0.78
c 0.85 0.82 0.78 0.76 0.93 0.83
d 0.76 0.93 0.83 0.88 0.93 0.72
e 0.88 0.93 0.72 0.12 0.15 0.20
f 0.12 0.15 0.20 0.44 0.10 0.28
g 0.44 0.10 0.28 0.61 0.09 0.84
h 0.61 0.09 0.84 0.74 0.87 0.69
i 0.74 0.87 0.69 0.38 0.23 0.44

我不确定以这种方式呈现布局,但这是我想要的信息。我正在寻找实现此目的的最有效方法。

到目前为止我做了什么

我尝试过以不同的方式使用 shift,但感觉很笨拙。这就是我用来生成上面输出的内容:

print pd.concat([df, df.shift(-1)], axis=1, keys=[0, 1]).dropna()

最佳答案

我们可以使用 NumPy 以其深奥的 strided tricks 获取那些滑动窗口的 View 。 .如果您使用这个新维度进行矩阵乘法等缩减,这将是理想的选择。如果出于某种原因,你想要一个 2D 输出,我们需要在最后使用 reshape,这将导致创建一个副本。

因此,实现看起来像这样 -

from numpy.lib.stride_tricks import as_strided as strided

def get_sliding_window(df, W, return2D=0):
a = df.values
s0,s1 = a.strides
m,n = a.shape
out = strided(a,shape=(m-W+1,W,n),strides=(s0,s0,s1))
if return2D==1:
return out.reshape(a.shape[0]-W+1,-1)
else:
return out

2D/3D 输出的样本运行 -

In [68]: df
Out[68]:
A B
0 0.44 0.41
1 0.46 0.47
2 0.46 0.02
3 0.85 0.82
4 0.78 0.76

In [70]: get_sliding_window(df, 3,return2D=1)
Out[70]:
array([[ 0.44, 0.41, 0.46, 0.47, 0.46, 0.02],
[ 0.46, 0.47, 0.46, 0.02, 0.85, 0.82],
[ 0.46, 0.02, 0.85, 0.82, 0.78, 0.76]])

这是 3D View 输出的样子 -

In [69]: get_sliding_window(df, 3,return2D=0)
Out[69]:
array([[[ 0.44, 0.41],
[ 0.46, 0.47],
[ 0.46, 0.02]],

[[ 0.46, 0.47],
[ 0.46, 0.02],
[ 0.85, 0.82]],

[[ 0.46, 0.02],
[ 0.85, 0.82],
[ 0.78, 0.76]]])

让我们为各种窗口大小的 View 3D 输出计时 -

In [331]: df = pd.DataFrame(np.random.rand(1000, 3).round(2))

In [332]: %timeit get_3d_shfted_array(df,2) # @Yakym Pirozhenko's soln
10000 loops, best of 3: 47.9 µs per loop

In [333]: %timeit get_sliding_window(df,2)
10000 loops, best of 3: 39.2 µs per loop

In [334]: %timeit get_3d_shfted_array(df,5) # @Yakym Pirozhenko's soln
10000 loops, best of 3: 89.9 µs per loop

In [335]: %timeit get_sliding_window(df,5)
10000 loops, best of 3: 39.4 µs per loop

In [336]: %timeit get_3d_shfted_array(df,15) # @Yakym Pirozhenko's soln
1000 loops, best of 3: 258 µs per loop

In [337]: %timeit get_sliding_window(df,15)
10000 loops, best of 3: 38.8 µs per loop

让我们验证我们确实获得了 View -

In [338]: np.may_share_memory(get_sliding_window(df,2), df.values)
Out[338]: True

即使在各种窗口大小下,get_sliding_window 的计时几乎不变,这表明获取 View 而不是复制的巨大好处。

关于python - 滚动窗口的数据帧表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37447347/

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