0

I have a numpy array such as this:

x = np.arange(0,9)
y = np.arange(20,29)
X = np.array([x, y])

so X looks like [[0,1,2,...9],[20,21,...,29]]

but I would like X to be shaped like this:

X = np.array([[0, 20],
          [1, 21],
          [2, 22],
          ...
          [9, 29]])

How can I do this with x, and y arrays given above?

2
  • 2
    try to transpose it: X.T Commented Aug 7, 2016 at 7:38
  • @MaxU please post as answer and I'll accept (I did try transpose before with a float dataset and must have done something wrong, works fine with the example I have shown) Commented Aug 7, 2016 at 7:43

2 Answers 2

1

you can transpose X to get desired result:

In [16]: X
Out[16]:
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8],
       [20, 21, 22, 23, 24, 25, 26, 27, 28]])

In [17]: X.T
Out[17]:
array([[ 0, 20],
       [ 1, 21],
       [ 2, 22],
       [ 3, 23],
       [ 4, 24],
       [ 5, 25],
       [ 6, 26],
       [ 7, 27],
       [ 8, 28]])
Sign up to request clarification or add additional context in comments.

Comments

0

Transpose the array:

x = np.arange(0,10)
y = np.arange(20,30)
X = np.vstack([x, y]).T

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.