将时间序列向量的2D矩阵重整为序列(帧)的3D矩阵-重叠窗口

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

我有一个51个时间序列矢量m样本的矩阵(形状:m 51)。我想训练两个自动编码器,一个使用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

的重叠生成序列。enter image description here

**

更新

**关于Split Python sequence (time series/array) into subsequences with overlap中的答案,使用函数[[sub-sequences将一维数组拆分为w个长子序列,重叠范围为[[w-1(步长为1),从而形成一个形状为2D的二维数组( m_new,w)。如代码2在下面,我必须使用循环将51个变量的每个向量作为1D数组工作,然后将2D数组的结果附加起来以生成最终的形状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
乘以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-1Demonstration of the output of Code 2 and Code 3
python recurrent-neural-network
1个回答
0
投票
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)

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