gpt4 book ai didi

python - vaex:将列移动 n 步

转载 作者:行者123 更新时间:2023-12-01 21:43:12 25 4
gpt4 key购买 nike

我正在为监督学习任务准备一个大型多变量时间序列数据集,我想创建我的输入特征的时移版本,以便我的模型也可以从过去的值中推断出来。 pandas 中有一个 shift(n) 命令,可让您将一列移动 n 行。 vaex 中有类似的东西吗?

我在 vaex 文档中找不到任何可比较的内容。

最佳答案

不,我们还不支持 (https://github.com/vaexio/vaex/issues/660)。因为 vaex 是可扩展的(请参阅 http://docs.vaex.io/en/latest/tutorial.html#Adding-DataFrame-accessors )我想我会以这种形式为您提供解决方案:

import vaex
import numpy as np

@vaex.register_dataframe_accessor('mytool', override=True)
class mytool:
def __init__(self, df):
self.df = df

def shift(self, column, n, inplace=False):
# make a copy without column
df = self.df.copy().drop(column)
# make a copy with just the colum
df_column = self.df[[column]]
# slice off the head and tail
df_head = df_column[-n:]
df_tail = df_column[:-n]
# stitch them together
df_shifted = df_head.concat(df_tail)
# and join (based on row number)
return df.join(df_shifted, inplace=inplace)

x = np.arange(10)
y = x**2
df = vaex.from_arrays(x=x, y=y)
df['shifted_y'] = df.y
df2 = df.mytool.shift('shifted_y', 2)
df2

它生成一个单列数据报,将其切片、连接并连接回去。所有这些都没有一个内存副本。

我在这里假设循环移位/循环。

关于python - vaex:将列移动 n 步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60995917/

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