简体   繁体   中英

I cannot 'join' 2 ndarrays of shape (rows,) into one ndarray of shape (rows,2)

np.MethodSeeBelow((raw_Scaled_CvOPct, raw_Scaled_CvMxPct))
  • I have tried np.hstack and get shape (rows * 2,)
  • I have tried np.concatenate and get shape (rows * 2,)
  • I have tried np.stack and get shape (2, rows)
  • I have tried np.vstack and get shape (2, rows)
  • I have tried np.dstack and get shape (1, rows, 2)

you can use stack along axis 1

n1 = np.random.random(10)
n2 = np.random.random(10)
n1.shape
> (10,)
s1 = np.stack((n1,n2), axis=1)
s1, s1.shape
> (array([[0.90308381, 0.76712636],
    [0.6700485 , 0.42458683],
    [0.53987017, 0.8661545 ],
    [0.31058594, 0.03774051],
    [0.06994416, 0.74861835],
    [0.70420554, 0.77298267],
    [0.4639175 , 0.37825594],
    [0.07486972, 0.11639835],
    [0.64662856, 0.20703329],
    [0.16519598, 0.55955276]]), (10, 2))

Any of the methods you tried can be used to get the desired result, with some slight tweaks:

  • np.hstack(((raw_Scaled_CvOPct , raw_Scaled_CvMxPct ))
  • np.concatenate(((raw_Scaled_CvOPct , raw_Scaled_CvMxPct ), )
  • np.stack(((raw_Scaled_CvOPct, raw_Scaled_CvMxPct), )
  • np.vstack(((raw_Scaled_CvOPct, raw_Scaled_CvMxPct))
  • np.dstack(((raw_Scaled_CvOPct, raw_Scaled_CvMxPct))
  • np.dstack(((raw_Scaled_CvOPct, raw_Scaled_CvMxPct))

The stack method suggested by @Oleg Butuzov is, in my opinion, the most numpythonic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM