如何将3D滑动窗口的3D阵列变成2D阵列



我正在使用Python(keras(中的自动编码器LSTM。我有一个多元输入,并且使用滑动窗口方法将其转换为LSTM输入的正确格式。最后,我获得的输出形状与窗口相同。然后,我想将此数组转换为原始输入形状。谁能帮我如何做?

这是我在多元信号上放置滑动窗口的代码:


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中的概念相同,您跳过以获取下一个窗口的元素数量。

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]])

相关内容

  • 没有找到相关文章

最新更新