【问题标题】:I have a Single column data. I want to reshape it so i can use this into RNN我有一个单列数据。我想重塑它,这样我就可以将它用于 RNN
【发布时间】:2023-04-04 13:22:01
【问题描述】:

我试图重塑它,但我遇到了错误。 在我必须重塑它之前,我必须使用 RNN。有什么想法吗?

X_train=np.reshape(Y,(Y.shape[0],Y.shape[1],1))

IndexError: 元组索引超出范围dataset

【问题讨论】:

    标签: python numpy machine-learning deep-learning deeplearning4j


    【解决方案1】:

    您的数据集很可能是一维的,而不是二维的。您的代码适用于 2D 就好了:

    import numpy as np
    
    Y = np.random.rand(3, 4)
    print(Y)
    
    Y = np.reshape(Y, (Y.shape[0], Y.shape[1], 1))
    print(Y)
    

    产量

    [[0.94716449 0.46469876 0.74290887 0.11051443]
     [0.31187829 0.26831897 0.37580931 0.23038081]
     [0.46578756 0.81175453 0.98348175 0.02975313]]
    
    [[[0.94716449]
      [0.46469876]
      [0.74290887]
      [0.11051443]]
    
     [[0.31187829]
      [0.26831897]
      [0.37580931]
      [0.23038081]]
    
     [[0.46578756]
      [0.81175453]
      [0.98348175]
      [0.02975313]]]
    

    【讨论】:

      【解决方案2】:

      reshape() 方法可以直接在numpy.array 上调用,即:

      Y = np.random.rand(3, 4)
      Y.reshape((Y.shape[0], Y.shape[1], 1))
      

      输出:

      array([[[0.03398233],
              [0.31845358],
              [0.26508794],
              [0.4154345 ]],
      
             [[0.80924495],
              [0.86116906],
              [0.24186532],
              [0.64169452]],
      
             [[0.61352962],
              [0.95214732],
              [0.26994666],
              [0.99091755]]])
      

      如果您的 Y 数组是 1D 的,您仍然可以像这样将其设为 3D:

      Y.reshape((1, 1, -1))
      

      输出:

      array([[[0.52130672]],
             [[0.25807463]],
             [[0.81201524]],
             [[0.08846268]],
             [[0.20831986]],
             [[0.823997  ]],
             [[0.483052  ]],
             [[0.15120415]],
             [[0.19601734]],
             [[0.55933897]],
             [[0.9112403 ]],
             [[0.1048653 ]]])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-03
        • 2021-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-10
        • 1970-01-01
        • 2021-02-03
        相关资源
        最近更新 更多