【问题标题】:Efficient way to Reshape Data for Time Series Prediction Machine Learning (Numpy)为时间序列预测机器学习重塑数据的有效方法(Numpy)
【发布时间】:2016-11-28 18:55:26
【问题描述】:

假设我有一个数据集(numpy 数组)X,包含 N 个时间序列样本,每个样本具有 D 维向量的 T 个时间步长,因此:

X.shape == (N,T,D)

现在我想将其重塑为 x(数据集)和 y(标签),以应用机器学习来预测时间序列中的步骤。

我想取每个长度为 n 的样本的每个子序列

x.shape==(N*(T-n),n,D) and y.shape==(N*(T-n)),D)

X[k,j:j+n,:]

是我在x

中的样本之一
X[k,j+n+1,:] 

y 中的标签。

for 循环是唯一的方法吗?

【问题讨论】:

    标签: python numpy machine-learning time-series reshape


    【解决方案1】:

    您正在寻找pandas data panel。 (http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Panel.html)。只需放入 numpy 数组,在短轴上转置并获得其 numpy 表示(.as_matrix() 或简单的.values)。如果你想真正只在 numpy 中做到这一点,numpy.transpose 只是为了 (https://docs.scipy.org/doc/numpy/reference/generated/numpy.transpose.html)

    【讨论】:

      【解决方案2】:

      所以我有以下方法,但它有一个for循环,我不确定我不能做得更好:

          def reshape_data(self, X, n):
          """
          Reshape a data set of N time series samples of T time steps each
          Args:
              data: Time series data of shape (N,T,D)
              n: int, length of time window used to predict x[t+1]
      
          Returns:
      
          """
          N,T,D = X.shape
      
          x = np.zeros((N*(T-n),n,D))
          y = np.zeros((N*(T-n),D))
      
          for i in range(T-n):
              x[N*i:N*(i+1),:,:] = X[:,i:i+n,:]
              y[N*i:N*(i+1),:] = X[:,i+n,:]
      
          return x,y
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-08
        • 2018-08-26
        • 2017-05-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多