将时间序列向量的 2D 矩阵重塑为序列(帧)的 3D 矩阵 - 重叠 windows
Reshaping a 2D matrix of Time series vectors into a 3D matrix of sequences (frames) - overlapping windows
我有一个矩阵(形状:m x 51),每个矩阵包含 51 个时间序列向量 m 个样本。我想训练两个自动编码器,一个使用 CNN,另一个使用 LSTM 网络。我想将 2D 矩阵重塑为 3D 矩阵,使其包含 51 个变量中每个变量的 m_new 序列,每个序列为 w 长 lap 样本重叠。
我成功完成了,但没有重叠部分。有什么有效的方法吗?
W = 20 #window size
m_new = int(np.floor(m/W))
m_trct = int(m_new*W)
X_raw_trct = X_raw[0:m_trct,:]
X = np.reshape(X_raw_trct,(m_new,W,X_raw_trct.shape[1]))
如下所示,序列是通过 lap = w-1.
的重叠生成的
** 更新 **
参考中的答案,
使用函数 sub-sequences 将 1D 数组拆分为 w 长子序列,重叠 w-1(步幅为 1)产生形状为 (m_new, w) 的二维数组。如 代码 2
下面,我不得不使用循环将 51 个变量的每个向量作为一维数组处理,然后附加二维数组的结果以生成最终的 3D 形状数组 (m_new, w, 51).但是,循环执行的时间太长了。
**code 2**
def subsequences(ts, window):
## ts is of shape (m,)
shape = (ts.size - window + 1, window)
strides = ts.strides * 2
return np.lib.stride_tricks.as_strided(ts, shape=shape, strides=strides)
# rescaledX_raw.shape is (m,51)
n = rescaledX_raw.shape[1]
# n = 51
a = rescaledX_raw[:,0]
# a.shape is (m,)
Xaa = subsequences(a,W)
X = ones(Xaa.shape)*-1
# X.shape is (m_new, W)
for kk in range(n):
## a is of shape (m,)
a = rescaledX_raw[:,kk]
Xaa = subsequences(a,W)
X = np.dstack((X, Xaa))
X_nn = np.delete(X, 0, axis=2)
# X_nn.shape is (m_new, W, 51)
此外,我尝试将其计算为一个完整的 2D 形状数组(m x 51)到 3D 形状数组(m_new,w,51) 使用代码3
中的函数
**code 3**
def rolling_window(a, window):
## a is of shape (51,m)
shape = (a.shape[-1] - window + 1,window,a.shape[0])
strides = a.strides + (a.strides[-1],)
return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
但是生成的 3D 矩阵不正确。请参考以下示范。此外,如何将步幅添加为我可以更改的变量。在上面的脚本中,步幅为 1(意味着重叠为 w-1)
我发现 post 可以使用 TimeseriesGenerator 来完成这项工作。
class CustomGenFit(TimeseriesGenerator):
def __getitem__(self, idx):
x, y = super().__getitem__(idx)
return x, x
Xsequences = CustomGenPredict(X, X, length=W, stride = s,sampling_rate=1, batch_size=m)
我有一个矩阵(形状:m x 51),每个矩阵包含 51 个时间序列向量 m 个样本。我想训练两个自动编码器,一个使用 CNN,另一个使用 LSTM 网络。我想将 2D 矩阵重塑为 3D 矩阵,使其包含 51 个变量中每个变量的 m_new 序列,每个序列为 w 长 lap 样本重叠。
我成功完成了,但没有重叠部分。有什么有效的方法吗?
W = 20 #window size
m_new = int(np.floor(m/W))
m_trct = int(m_new*W)
X_raw_trct = X_raw[0:m_trct,:]
X = np.reshape(X_raw_trct,(m_new,W,X_raw_trct.shape[1]))
如下所示,序列是通过 lap = w-1.
的重叠生成的** 更新 **
参考
**code 2**
def subsequences(ts, window):
## ts is of shape (m,)
shape = (ts.size - window + 1, window)
strides = ts.strides * 2
return np.lib.stride_tricks.as_strided(ts, shape=shape, strides=strides)
# rescaledX_raw.shape is (m,51)
n = rescaledX_raw.shape[1]
# n = 51
a = rescaledX_raw[:,0]
# a.shape is (m,)
Xaa = subsequences(a,W)
X = ones(Xaa.shape)*-1
# X.shape is (m_new, W)
for kk in range(n):
## a is of shape (m,)
a = rescaledX_raw[:,kk]
Xaa = subsequences(a,W)
X = np.dstack((X, Xaa))
X_nn = np.delete(X, 0, axis=2)
# X_nn.shape is (m_new, W, 51)
此外,我尝试将其计算为一个完整的 2D 形状数组(m x 51)到 3D 形状数组(m_new,w,51) 使用代码3
中的函数**code 3**
def rolling_window(a, window):
## a is of shape (51,m)
shape = (a.shape[-1] - window + 1,window,a.shape[0])
strides = a.strides + (a.strides[-1],)
return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
但是生成的 3D 矩阵不正确。请参考以下示范。此外,如何将步幅添加为我可以更改的变量。在上面的脚本中,步幅为 1(意味着重叠为 w-1)
我发现 post 可以使用 TimeseriesGenerator 来完成这项工作。
class CustomGenFit(TimeseriesGenerator):
def __getitem__(self, idx):
x, y = super().__getitem__(idx)
return x, x
Xsequences = CustomGenPredict(X, X, length=W, stride = s,sampling_rate=1, batch_size=m)