使用应用到段时间序列数据帧大熊猫?

问题描述 投票:0回答:1

我feading时间序列成Keras LSTM模型和我有形状(586075,30,8)的所希望的输入。我现在做的方法是:

# DataFrame input shape
print(train_features.shape)
(586105, 8)

x_train = []
y_train = []
t_steps = 30
# Segmenting Data
for i in range(t_steps, train_features.shape[0]):
    x_train.append(train_features[i-t_steps:i,:] )#upper bound in slice excluded
    y_train.append(train_targets[i-1,:] ) 
x_train, y_train = np.array(x_train), np.array(y_train)

print(x_train.shape, y_train.shape)
(586075,30,8) (586075,8)

这对大型数据集慢得令人难以置信,我知道什么时候能够避免它,你不应该在循环大熊猫。有没有办法做到这一点使用应用或与大熊猫或numpy的其他更有效的方法?

数据的最后一列是一天当中的片段。我用这个掩蔽数据,以便在每个样品中的最后一个时间步骤是两个时间之间:

# Filter for Times between 9:30 and 13:00
mask = np.where((x_train[:,-1,-1] > .3958) & (x_train[:,-1,-1] < .541667))
x_train = x_train[mask,:,:][0]
y_train = y_train[mask,:][0]

看起来这可以在一个步骤中全部完成,更有效地使用应用或数据发生器。

python pandas numpy keras time-series
1个回答
2
投票

您可以预先分配输出numpy的阵列和遍历小t_steps尺寸加快了一点东西。 for循环Python中是几乎没有坏,如果他们保持较小。

def add_windowed_dim(arr, window_size):
    out = np.empty((window_size, arr.shape[0]-window_size, arr.shape[1]))        
    for i in range(window_size):                              
        out[i, :, :] = arr[i:-window_size+i, :]
    return np.rollaxis(out, 1)

x_train = add_windowed_dim(train_features, t_steps)
y_train = train_targets[t_steps-1:-1, :]

这改善了在您使用相同大小的模拟数据集从1350毫秒到110毫秒的运行时间。

第二步看起来好像没什么问题。它的运行时间已经在77毫秒相对较短。

© www.soinside.com 2019 - 2024. All rights reserved.