gpt4 book ai didi

python - lstm 时间序列的多步序列的数据转换

转载 作者:行者123 更新时间:2023-12-01 00:26:35 31 4
gpt4 key购买 nike

我有一个如下所示的数据框,我想在客户级别创建多序列数据。请帮助我。

请注意,一个客户可以有 10 行,其他客户可以有 14 行。

数据框(输入):

Date    Customer    Price
1/6/2019 A 855
1/7/2019 A 989
1/8/2019 A 454
1/9/2019 A 574
1/10/2019 A 395
1/1/2019 A 162
1/2/2019 A 123
1/3/2019 A 342
1/4/2019 A 232
1/5/2019 A 657
1/6/2019 B 875
1/7/2019 B 999
1/8/2019 B 434
1/9/2019 B 564
1/10/2019 B 345
1/10/2019 B 798
1/6/2019 B 815
1/7/2019 B 929
1/8/2019 B 444
1/9/2019 B 554
1/10/2019 B 395
1/10/2019 B 768

输出:

X_data(independent variables)   y_data(target)
customer 0 1 2 3 0 1
A 855 989 454 574 395 162
A 989 454 574 395 162 123
A 454 574 395 162 123 342
A 574 395 162 123 342 232
A 395 162 123 342 232 657
B 875 999 434 564 345 798
B 999 434 564 345 798 875
B 434 564 345 798 564 345
B 564 345 798 815 929 444
B 345 798 815 929 444 554
B 798 815 929 444 554 395
B 815 929 444 554 395 768

从长远来看,X_data 可以扩展到 70 列,y_dat 可以扩展到 40 列。我尝试了下面的 for 循环,无论客户如何,但需要进行一些修改才能在客户级别上使用它。

data = np.array(data)
X_data, y_data = [], []
for i in range(4, len(data )-2):
X_data.append(data[i-4:i])
y_data.append(data[i:i+2])

请对此提出建议。提前致谢

最佳答案

这是我的建议。

首先将数据排序为价格序列(忽略日期并假设原始列表按时间顺序排序):

df = df.reset_index()
prices_by_customers = df.groupby('Customer')['Price']
customers = [name for name, prices in prices_by_customers]
price_sequences = pd.concat([prices.reset_index(drop=True)
for name, prices in prices_by_customers],
axis=1, keys=customers)

价格序列:

        A    B
0 855.0 875
1 989.0 999
2 454.0 434
3 574.0 564
4 395.0 345
5 162.0 798
6 123.0 815
7 342.0 929
8 232.0 444
9 657.0 554
10 NaN 395
11 NaN 768

现在,创建您想要的数据框:

# Add shifted columns
x_data = []
y_data = []
for customer, prices in prices_by_customers:
x = {i: prices.shift(-i) for i in range(4)}
y = {i: prices.shift(-i-4) for i in range(2)}
x['Customer'] = customer
y['Customer'] = customer
x_data.append(pd.DataFrame(x))
y_data.append(pd.DataFrame(y))

# Join up datasets
x_data = pd.concat(x_data)
y_data = pd.concat(y_data)
print(pd.concat([x_data, y_data], axis=1))

输出:

      0      1      2      3 Customer      0      1 Customer
0 855 989.0 454.0 574.0 A 395.0 162.0 A
1 989 454.0 574.0 395.0 A 162.0 123.0 A
2 454 574.0 395.0 162.0 A 123.0 342.0 A
3 574 395.0 162.0 123.0 A 342.0 232.0 A
4 395 162.0 123.0 342.0 A 232.0 657.0 A
5 162 123.0 342.0 232.0 A 657.0 NaN A
6 123 342.0 232.0 657.0 A NaN NaN A
7 342 232.0 657.0 NaN A NaN NaN A
8 232 657.0 NaN NaN A NaN NaN A
9 657 NaN NaN NaN A NaN NaN A
10 875 999.0 434.0 564.0 B 345.0 798.0 B
11 999 434.0 564.0 345.0 B 798.0 815.0 B
12 434 564.0 345.0 798.0 B 815.0 929.0 B
13 564 345.0 798.0 815.0 B 929.0 444.0 B
14 345 798.0 815.0 929.0 B 444.0 554.0 B
15 798 815.0 929.0 444.0 B 554.0 395.0 B
16 815 929.0 444.0 554.0 B 395.0 768.0 B
17 929 444.0 554.0 395.0 B 768.0 NaN B
18 444 554.0 395.0 768.0 B NaN NaN B
19 554 395.0 768.0 NaN B NaN NaN B
20 395 768.0 NaN NaN B NaN NaN B
21 768 NaN NaN NaN B NaN NaN B

最后,删除 NaN 等:

training_data = pd.concat([x_data, y_data], axis=1, keys=['X_data', 'y_data']).dropna()
training_data = training_data.reset_index(drop=True).drop(('y_data', 'Customer'), axis=1)

训练数据:

   X_data                               y_data       
0 1 2 3 Customer 0 1
0 855.0 989.0 454.0 574.0 A 395.0 162.0
1 989.0 454.0 574.0 395.0 A 162.0 123.0
2 454.0 574.0 395.0 162.0 A 123.0 342.0
3 574.0 395.0 162.0 123.0 A 342.0 232.0
4 395.0 162.0 123.0 342.0 A 232.0 657.0
5 875.0 999.0 434.0 564.0 B 345.0 798.0
6 999.0 434.0 564.0 345.0 B 798.0 815.0
7 434.0 564.0 345.0 798.0 B 815.0 929.0
8 564.0 345.0 798.0 815.0 B 929.0 444.0
9 345.0 798.0 815.0 929.0 B 444.0 554.0
10 798.0 815.0 929.0 444.0 B 554.0 395.0
11 815.0 929.0 444.0 554.0 B 395.0 768.0

关于python - lstm 时间序列的多步序列的数据转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58544615/

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