代码之家  ›  专栏  ›  技术社区  ›  Raven Cheuk

如何使用sklearn规范化具有多个特征的时间序列数据?

  •  0
  • Raven Cheuk  · 技术社区  · 7 年前

    对于具有形状的数据 (num_samples,features) , MinMaxScaler sklearn.preprocessing

    但是,当用同样的方法对具有形状的时间序列数据进行处理时 (num_samples, time_steps,features) ,sklearn将给出一个错误。

    from sklearn.preprocessing import MinMaxScaler
    import numpy as np
    
    #Making artifical time data
    x1 = np.linspace(0,3,4).reshape(-1,1)
    x2 = np.linspace(10,13,4).reshape(-1,1)
    X1 = np.concatenate((x1*0.1,x2*0.1),axis=1)
    X2 = np.concatenate((x1,x2),axis=1)
    X = np.stack((X1,X2))
    
    #Trying to normalize
    scaler = MinMaxScaler()
    X_norm = scaler.fit_transform(X) <--- error here
    

    ValueError:找到了dim为3的数组。需要MinMaxScaler<=2.

    这个 post 有点像

    (timeseries-timeseries.min())/(timeseries.max()-timeseries.min())
    

    然而,它只适用于只有一个特性的数据。因为我的数据有不止一个特征,所以这个方法不起作用。

    如何规范化具有多个特征的时间序列数据?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Nagabhushan Baddi    7 年前

    要规范化形状的三维张量(n个采样、时间步、n个特征),请使用以下命令:

    (timeseries-timeseries.min(axis=2))/(timeseries.max(axis=2)-timeseries.min(axis=2))
    

    使用参数axis=2将返回沿第三维(即特征轴)执行的张量运算的结果。因此,每个特征将被独立地标准化。

    推荐文章