notIntoXS
notIntoXS

Reputation: 129

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

np.MethodSeeBelow((raw_Scaled_CvOPct, raw_Scaled_CvMxPct))

Upvotes: 1

Views: 31

Answers (2)

Mad Physicist
Mad Physicist

Reputation: 114320

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

  • np.hstack(((raw_Scaled_CvOPct[:, None], raw_Scaled_CvMxPct[:, None]))
  • np.concatenate(((raw_Scaled_CvOPct[:, None], raw_Scaled_CvMxPct[:, None]), axis=1)
  • np.stack(((raw_Scaled_CvOPct, raw_Scaled_CvMxPct), axis=1)
  • np.vstack(((raw_Scaled_CvOPct, raw_Scaled_CvMxPct)).T
  • np.dstack(((raw_Scaled_CvOPct, raw_Scaled_CvMxPct))[0]
  • np.dstack(((raw_Scaled_CvOPct, raw_Scaled_CvMxPct)).reshape(-1, 2)

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

Upvotes: 0

Oleg Butuzov
Oleg Butuzov

Reputation: 5395

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))

Upvotes: 1

Related Questions