【问题标题】:How to solve error when reshaping DataFrame to LSTM将 DataFrame 重塑为 LSTM 时如何解决错误
【发布时间】:2022-01-11 08:52:06
【问题描述】:

我有这样的形状 (6042, 6) 的 CSV 数据,并使用 DataFrame 调用它:

我想将它用于 LSTM 的输入。 根据我的阅读,LSTM 需要 3d 数组 [samples, time steps, features] 形式的数据。 因为DataFrame没有reshape属性,我改成NumPy数组,尝试reshape,但是报错:

TypeError: only integer scalar arrays can be converted to a scalar index

我尝试使用以下代码:

train_data = train_data.to_numpy()
train_data = train_data.reshape(1, train_data[0], train_data[1])

我的步骤错了吗?

【问题讨论】:

    标签: python dataframe numpy deep-learning lstm


    【解决方案1】:

    您的代码返回一条错误消息,因为当您编写 train_data[0] 时,您将获得 train_data 二维 numpy 数组的第一行:

    >>> df = pd.DataFrame([[.4,.6,.3], [.7,.8,.9]])
    >>> df
         0    1    2
    0  0.4  0.6  0.3
    1  0.7  0.8  0.9
    >>> df = df.to_numpy()
    >>> df[0]
    array([0.4, 0.6, 0.3])
    
    

    您真正想要的是使用数据框的形状。试试这个:

    >>> df = df.to_numpy()
    >>> df = df.reshape(1, df.shape[0], df.shape[1])
    >>> df
    array([[[0.4, 0.6, 0.3],
            [0.7, 0.8, 0.9]]])
    >>> df.shape
    (1, 2, 3)
    

    【讨论】:

      【解决方案2】:

      基于reshape documentation,调用例程的格式为:

      numpy.reshape(a, newshape, order='C')
      

      因此,你应该这样做……

      import numpy    
      numpy.reshape(train_data, (1, train_data[0], train_data[1]))
      

      ...假设上面的形状是你想要的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-18
        • 1970-01-01
        • 1970-01-01
        • 2019-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多