0

I'm trying to convert

array([0, 1, 2, 3, 4, 5]) 

to

array([[0,1,2,3,4,5]])

But I can't find any information online. Are there a any solution to this?

5
  • np.expand_dims Commented Oct 9, 2019 at 20:10
  • It doesn't seem to be working, I used np.array() to wrap it but it just left me with what I had previously Commented Oct 9, 2019 at 20:11
  • 3
    You want either np.expand_dims(a, 0) or a[None], or maybe np.atleast_2d(a) if you want to handle scalars and arrays that already are 2D Commented Oct 9, 2019 at 20:12
  • @user3483203 I think this should be an answer Commented Oct 9, 2019 at 20:14
  • reshape(arr, (1,5)) is also useful. Look at the shape of the desired array Commented Oct 9, 2019 at 20:51

1 Answer 1

1
a = np.array([1,2,3,4,5])
a[None]
Sign up to request clarification or add additional context in comments.

1 Comment

docs.scipy.org/doc/numpy/reference/arrays.indexing.html The newaxis object can be used in all slicing operations to create an axis of length one. newaxis is an alias for ‘None’, and ‘None’ can be used in place of this with the same result.

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.