Home > database >  Laminating two column arrays
Laminating two column arrays

Time:12-15

Assume I have two numpy (N, 1) column arrays. I know their length to be equal:

>>> a
array([[0.],
       [2.],
       [4.],
       [6.]])
>>> b
array([[0.],
       [1.],
       [2.],
       [3.]])

and I'd like to "laminate" them side by side to form an (N, 2) array. The following works:

>>> np.array((a, b)).reshape(2, len(a)).transpose()
array([[0., 0.],
       [2., 1.],
       [4., 2.],
       [6., 3.]])

... but is there a simpler, more direct way to accomplish this?

CodePudding user response:

You can just stack them horizontally using np.hstack (docs)

>>> np.hstack((a, b))
array([[0., 0.],
       [2., 1.],
       [4., 2.],
       [6., 3.]]
  • Related