代码之家  ›  专栏  ›  技术社区  ›  8-Bit Borges

将序列转换为numpy数组,并将其与不同维数的数组连接起来

  •  0
  • 8-Bit Borges  · 技术社区  · 3 年前

    x_train 形状如下(3800,10):

    [[ 8.58786106  2.05531597 47.69544082 ... 22.18313066 58.95737578
      16.74565073]
     [ 9.12246609  2.22890568 54.10625074 ... 26.04059537 56.0996469
      18.67649081]
     [ 8.69304657  1.68278122 49.72647589 ... 27.39661295 55.67941362
      21.72374764]
     ...
     [ 7.38825607  1.9323082  42.25193966 ... 26.32971473 76.62631333
      23.39134685]
     [ 9.36668968  2.21513939 51.51743298 ... 15.17773412 48.62399762
      18.85870167]
     [ 6.85041714  1.48223543 48.24985089 ... 50.06416883 96.68290317
      45.1459154 ]]
    

    0       1.0
    1       0.0
    2       1.0
    3      -1.0
    4      -1.0
           ... 
    3795    0.0
    3796   -1.0
    3797    1.0
    3798   -1.0
    3799   -1.0
    

    如何将此系列格式化为一个数组,并按以下方式连接它们?

    xy_train_model = np.concatenate([x_train, y_train], axis=1)
    

    所以我最终得到了(3800,11)的形状:

    [[ 8.58786106  2.05531597 47.69544082 ... 22.18313066 58.95737578
      16.74565073, 1.0]
     [ 9.12246609  2.22890568 54.10625074 ... 26.04059537 56.0996469
      18.67649081, 0.0]
     ...
    
    2 回复  |  直到 3 年前
        1
  •  0
  •   Reti43    3 年前
    # the `None` is to broadcast it to (3800, 1) shape
    y_train = series.to_numpy()[:,None]
    

    np.hstack([x_train, y_train])

        2
  •  1
  •   Vibhav Surve    3 年前

    pandas.DataFrame 然后创建一个新列并添加y\u train。如果需要,可以稍后将其转换为数组。