gpt4 book ai didi

python - 向量化前瞻性函数 pandas 数据框

转载 作者:太空狗 更新时间:2023-10-30 01:38:11 24 4
gpt4 key购买 nike

我想在 pandas 中对 DataFrame(可以被认为是一个系列)进行“奇怪”的计算。 DataFrame 必须被视为时间序列或类似的(元素的顺序很重要)。

  • 给定索引[i] (value[i]) 的值
  • 给定一个步骤(例如 1)[整数或实数]
  • 给定一个乘数 rr(例如 2)[整数或实数]

向前看元素 [i:] 并为值 [i] 分配一个“类别”:

  • 如果随后的值达到 value[i] + step * rr before 达到 value[i] - step 的水平,则 +1
  • -1 如果随后的值达到 value[i] 的水平 - step * rr before reaching value[i] + step
  • 在其他情况下为 0(即当后续值触及值 [i] - 步骤然后值[i] + 步骤或反之亦然。

我知道这听起来很疯狂。想象一下 +1/-1 步的随机行走。像这样的序列:

  • 0, 1, 2 会被分配到类+1(也可以是0, 1, 0, 0, 1, 1, 0, 1, 1, 2)
  • 0,-1,-2会被分配到类-1(也可以是0,-1,0,0,0,-1,-1,-1,-2)
  • 0、+1、0、-1 或 0、-1、0、0、-1、0、1 等将是 0 类。

我通过定义一个函数以“经典”(也许不是那么 pythonic)的方式解决了这个问题:

import numpy as np
import pandas as pd

def FindClass(inarr, i=0, step=0.001, rr=2):
j = 0
foundClass = None
while i+j < len(inarr) - 1:
j += 1
if inarr[i+j] >= inarr[i] + step:
direction = 1
break
if inarr[i+j] <= inarr[i] - step:
direction = -1
break

while i+j < len(inarr)-1:
j += 1
if direction == 1 and inarr[i+j] >= inarr[i] + (step * rr):
foundClass = 1
break
elif direction == 1 and inarr[i+j] <= inarr[i] - step:
foundClass = 0
break
elif direction == -1 and inarr[i+j] <= inarr[i] - (step * rr):
foundClass = -1
break
elif direction == -1 and inarr[i+j] >= inarr[i] + step:
foundClass = 0
break
if foundClass is None:
foundClass = np.nan

return foundClass

然后遍历它:

if __name__ == "__main__":
steps = np.random.randint(-1, 2, size= 10000)

randomwalk = steps.cumsum(0)
rc = pd.DataFrame({'rw':randomwalk, 'result': np.nan})

for c in range(0, len(rc)-1):
rc.result[c] = FindClass(rc.rw, i=c, step=1)

print rc

在我的笔记本电脑(并运行 python 2.7)上,我得到了一个对于 10000 个元素的系列来说还算不错的分析:

python -m cProfile -s cumulative fbmk.py
<class 'pandas.core.frame.DataFrame'>
Int64Index: 10000 entries, 0 to 9999
Data columns (total 2 columns):
result 9996 non-null values
rw 10000 non-null values
dtypes: float64(1), int32(1)
932265 function calls (929764 primitive calls) in 2.643 seconds

Ordered by: cumulative time

ncalls tottime percall cumtime percall filename:lineno(function)
1 0.106 0.106 2.646 2.646 fbmk.py:1(<module>)
9999 0.549 0.000 1.226 0.000 fbmk.py:4(FindClass)
158062 0.222 0.000 0.665 0.000 series.py:616(__getitem__)
2 0.029 0.014 0.561 0.281 __init__.py:3(<module>)
158062 0.226 0.000 0.443 0.000 index.py:718(get_value)
19998 0.070 0.000 0.442 0.000 frame.py:2082(__getattr__)
19998 0.111 0.000 0.331 0.000 frame.py:1986(__getitem__)

问题是:

有没有人看到以提高性能的方式在 pandas/numpy 中矢量化此函数的可能性?

如果这件事在 R 中可以用更少的努力完成,那也很好!

提前致谢!

最佳答案

根据您的问题的属性,您可以使用 np.where 找到水平交叉的位置并对时间序列进行分类。

这里最大的缺点是 np.where 会为您提供时间序列高于 value[i] + step 等的每个索引,这可能会将线性时间算法转换为二次时间算法。根据您将要处理的问题的大小,我希望您能在预因子中收获很多;您甚至可能会领先于二次时间 numpy 解决方案。

通过四处寻找,与 np.where 等效的“找到第一个索引”仍然是一个请求的功能。

关于python - 向量化前瞻性函数 pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23303525/

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