序列到序列和迭代
Sequence to Sequence Sum Iteration
我正在寻找有关如何生成具有从前一个序列的 end/last 开始的累积和的序列的方向。
n_pre 序列是根据原始数据的滚动总和构建的。 n_post 序列需要使用来自正确点的原始数据值来构建 n_pre 序列的最终总和在索引中。
import pandas as pd
import numpy as np
# sample data
data = np.array([10, -1, 10, 10, -1, -10, 5, -5, -5, 10, 10, 5, -10, 10, 5, 10, -5, 10])
# load data into Pandas DataFrame
df = pd.DataFrame(data=data, columns=['data'])
# perform rolling sum calculation on data
df['roll_sum'] = df.rolling(5).sum().fillna(0)
# prepare data for sequences
X = np.array(df.roll_sum)
Y = np.array(df.data) # Y array and sequences need to sum the values from the end/last of the n_pre
sequence
dX, dY = [], []
n_pre = 5
n_post = 3
for i in range(len(X) - n_pre - n_post):
dX.append(X[i:i + n_pre])
dY.append((Y[i + n_pre:i + n_pre + n_post]))
dataX = np.array(dX)
#dataX = np.reshape(dataX, (-1, n_pre, 1))
dataY = np.array(dY)
#dataY = np.reshape(dataY, (-1, n_post, 1))
第一个 dataX 序列将生成:
[[ 0. 0. 0. 0. 28.]
第一个 dataY 序列将生成:
[[-10 5 -5]
我希望 dataY 序列生成这个:
[[18 23 18]]
计算(从 dataX 序列的末尾开始)28 -10 +5 -5 = 18 作为该数组中的最终值。
我创建了变量 a、b、c、d 和 e。
- c 基于 a 和 b
- d 基于 c 和 b
- e 基于 d 和 b
- 然后,根据您在问题末尾和评论中定义的逻辑,我们将每行的 c、d 和 e 添加到 for 循环中的
dY
。
代码:
import pandas as pd
import numpy as np
# sample data
data = np.array([10, -1, 10, 10, -1, -10, 5, -5, -5, 10, 10, 5, -10, 10, 5, 10, -5, 10])
# load data into Pandas DataFrame
df = pd.DataFrame(data=data, columns=['data'])
# perform rolling sum calculation on data
df['roll_sum'] = df.rolling(5).sum().fillna(0)
# prepare data for sequences
X = np.array(df.roll_sum)
Y = np.array(df.data) # Y array and sequences need to sum the values from the end/last of the n_pre
dX, dY = [], []
n_pre = 5
n_post = 3
for i in range(len(X) - n_pre - n_post):
a = X[i:i + n_pre]
b = Y[i + n_pre:i + n_pre + n_post]
c = a[-1] + b[0]
d = c + b[1]
e = d + b[2]
dX.append(a)
dY.append([c, d, e])
dataX = np.array(dX)
#dataX = np.reshape(dataX, (-1, n_pre, 1))
dataY = np.array(dY)
#dataY = np.reshape(dataY, (-1, n_post, 1))
dataY
dataY
输出:
array([[18., 23., 18.],
[13., 8., 3.],
[ 9., 4., 14.],
[-6., 4., 14.],
[-6., 4., 9.],
[ 5., 10., 0.],
[20., 10., 20.],
[ 5., 15., 20.],
[20., 25., 35.],
[30., 40., 35.]])
我正在寻找有关如何生成具有从前一个序列的 end/last 开始的累积和的序列的方向。
n_pre 序列是根据原始数据的滚动总和构建的。 n_post 序列需要使用来自正确点的原始数据值来构建 n_pre 序列的最终总和在索引中。
import pandas as pd
import numpy as np
# sample data
data = np.array([10, -1, 10, 10, -1, -10, 5, -5, -5, 10, 10, 5, -10, 10, 5, 10, -5, 10])
# load data into Pandas DataFrame
df = pd.DataFrame(data=data, columns=['data'])
# perform rolling sum calculation on data
df['roll_sum'] = df.rolling(5).sum().fillna(0)
# prepare data for sequences
X = np.array(df.roll_sum)
Y = np.array(df.data) # Y array and sequences need to sum the values from the end/last of the n_pre
sequence
dX, dY = [], []
n_pre = 5
n_post = 3
for i in range(len(X) - n_pre - n_post):
dX.append(X[i:i + n_pre])
dY.append((Y[i + n_pre:i + n_pre + n_post]))
dataX = np.array(dX)
#dataX = np.reshape(dataX, (-1, n_pre, 1))
dataY = np.array(dY)
#dataY = np.reshape(dataY, (-1, n_post, 1))
第一个 dataX 序列将生成:
[[ 0. 0. 0. 0. 28.]
第一个 dataY 序列将生成:
[[-10 5 -5]
我希望 dataY 序列生成这个:
[[18 23 18]]
计算(从 dataX 序列的末尾开始)28 -10 +5 -5 = 18 作为该数组中的最终值。
我创建了变量 a、b、c、d 和 e。
- c 基于 a 和 b
- d 基于 c 和 b
- e 基于 d 和 b
- 然后,根据您在问题末尾和评论中定义的逻辑,我们将每行的 c、d 和 e 添加到 for 循环中的
dY
。
代码:
import pandas as pd
import numpy as np
# sample data
data = np.array([10, -1, 10, 10, -1, -10, 5, -5, -5, 10, 10, 5, -10, 10, 5, 10, -5, 10])
# load data into Pandas DataFrame
df = pd.DataFrame(data=data, columns=['data'])
# perform rolling sum calculation on data
df['roll_sum'] = df.rolling(5).sum().fillna(0)
# prepare data for sequences
X = np.array(df.roll_sum)
Y = np.array(df.data) # Y array and sequences need to sum the values from the end/last of the n_pre
dX, dY = [], []
n_pre = 5
n_post = 3
for i in range(len(X) - n_pre - n_post):
a = X[i:i + n_pre]
b = Y[i + n_pre:i + n_pre + n_post]
c = a[-1] + b[0]
d = c + b[1]
e = d + b[2]
dX.append(a)
dY.append([c, d, e])
dataX = np.array(dX)
#dataX = np.reshape(dataX, (-1, n_pre, 1))
dataY = np.array(dY)
#dataY = np.reshape(dataY, (-1, n_post, 1))
dataY
dataY
输出:
array([[18., 23., 18.],
[13., 8., 3.],
[ 9., 4., 14.],
[-6., 4., 14.],
[-6., 4., 9.],
[ 5., 10., 0.],
[20., 10., 20.],
[ 5., 15., 20.],
[20., 25., 35.],
[30., 40., 35.]])