如何将滑动 window 的 3d 数组展平为 2d 数组?
How to flatten a 3d array of sliding window into 2d array?
我在 python(Keras) 中使用自动编码器 LSTM。我有一个多变量输入,我使用滑动 window 方法将其转换为 LSTM 输入的正确格式。最后,我得到了与 window 形状相同的输出。
然后我想将这个数组转换为原始输入形状。任何人都可以帮助我我应该怎么做吗?
这是我在多变量信号上放置滑动 window 的代码:
def window(samples, windows_size, step):
m, n = samples.shape
print("\nold shape: ", m, "*", n)
num_signals = n
num_samples = (samples.shape[0] - windows_size) // step + 1
aa = np.empty([num_samples, windows_size, num_signals])
for j in range(num_samples):
for i in range(num_signals):
aa[j, :, i] = samples[(j * step):(j * step + windows_size), i]
samples = aa
m ,n, k = samples.shape
print("new shape: ", m, "*", n, "*", k)
return samples
x = np.asarray([[1,0.1,0.1],[2,0.2,0.2],[3,0.3,0.3],[4,0.4,0.4],
[5,0.5,0.5],[6,0.6,0.6],[7,0.7,0.7],[8,0.8,0.8]])
window(x, 3, 2)
old shape: 8 * 3
new shape: 3 * 3 * 3
Out[65]:
array([[[1. , 0.1, 0.1],
[2. , 0.2, 0.2],
[3. , 0.3, 0.3]],
[[3. , 0.3, 0.3],
[4. , 0.4, 0.4],
[5. , 0.5, 0.5]],
[[5. , 0.5, 0.5],
[6. , 0.6, 0.6],
[7. , 0.7, 0.7]]])
你可以使用这个:
注意:步幅与 CNN 中的概念相同,您跳过以获得下一个元素的数量 window。
inp = np.array([[[1. , 0.1, 0.1],
[2. , 0.2, 0.2],
[3. , 0.3, 0.3]],
[[3. , 0.3, 0.3],
[4. , 0.4, 0.4],
[5. , 0.5, 0.5]],
[[5. , 0.5, 0.5],
[6. , 0.6, 0.6],
[7. , 0.7, 0.7]]])
def restitch(array, stride):
flat = array.flatten().reshape(-1,array.shape[2])
keep = [i for i in range(len(flat)) if not(i%(stride+1)==0 and i>0)]
return flat[keep]
restitch(inp, 2)
array([[1. , 0.1, 0.1],
[2. , 0.2, 0.2],
[3. , 0.3, 0.3],
[4. , 0.4, 0.4],
[5. , 0.5, 0.5],
[6. , 0.6, 0.6],
[7. , 0.7, 0.7]])
我在 python(Keras) 中使用自动编码器 LSTM。我有一个多变量输入,我使用滑动 window 方法将其转换为 LSTM 输入的正确格式。最后,我得到了与 window 形状相同的输出。 然后我想将这个数组转换为原始输入形状。任何人都可以帮助我我应该怎么做吗?
这是我在多变量信号上放置滑动 window 的代码:
def window(samples, windows_size, step):
m, n = samples.shape
print("\nold shape: ", m, "*", n)
num_signals = n
num_samples = (samples.shape[0] - windows_size) // step + 1
aa = np.empty([num_samples, windows_size, num_signals])
for j in range(num_samples):
for i in range(num_signals):
aa[j, :, i] = samples[(j * step):(j * step + windows_size), i]
samples = aa
m ,n, k = samples.shape
print("new shape: ", m, "*", n, "*", k)
return samples
x = np.asarray([[1,0.1,0.1],[2,0.2,0.2],[3,0.3,0.3],[4,0.4,0.4],
[5,0.5,0.5],[6,0.6,0.6],[7,0.7,0.7],[8,0.8,0.8]])
window(x, 3, 2)
old shape: 8 * 3
new shape: 3 * 3 * 3
Out[65]:
array([[[1. , 0.1, 0.1],
[2. , 0.2, 0.2],
[3. , 0.3, 0.3]],
[[3. , 0.3, 0.3],
[4. , 0.4, 0.4],
[5. , 0.5, 0.5]],
[[5. , 0.5, 0.5],
[6. , 0.6, 0.6],
[7. , 0.7, 0.7]]])
你可以使用这个:
注意:步幅与 CNN 中的概念相同,您跳过以获得下一个元素的数量 window。
inp = np.array([[[1. , 0.1, 0.1],
[2. , 0.2, 0.2],
[3. , 0.3, 0.3]],
[[3. , 0.3, 0.3],
[4. , 0.4, 0.4],
[5. , 0.5, 0.5]],
[[5. , 0.5, 0.5],
[6. , 0.6, 0.6],
[7. , 0.7, 0.7]]])
def restitch(array, stride):
flat = array.flatten().reshape(-1,array.shape[2])
keep = [i for i in range(len(flat)) if not(i%(stride+1)==0 and i>0)]
return flat[keep]
restitch(inp, 2)
array([[1. , 0.1, 0.1],
[2. , 0.2, 0.2],
[3. , 0.3, 0.3],
[4. , 0.4, 0.4],
[5. , 0.5, 0.5],
[6. , 0.6, 0.6],
[7. , 0.7, 0.7]])