34

What is the best way to convert a vector to a 2-dimensional array?

For example, a vector b of size (10, )

a = rand(10,10)
b = a[1, :]
b.shape

Out: (10L,)

can be converted to array of size (10,1) as

b = b.reshape(len(b), 1)

Is there a more concise way to do it?

4
  • 3
    (10,1) is a 2d array. Commented Nov 2, 2015 at 15:37
  • @hpaulj I'm guessing OP meant "convert 1d numpy array to column vector"... Commented Nov 2, 2015 at 15:41
  • 1
    In stackoverflow.com/questions/28385666/… compares and contrasts reshape and newaxis. Commented Nov 2, 2015 at 16:49
  • 1
    I took the liberty to edit the title and text to say 2d. Commented Nov 2, 2015 at 16:52

5 Answers 5

33

Since you lose a dimension when indexing with a[1, :], the lost dimension needs to be replaced to maintain a 2D shape. With this in mind, you can make the selection using the syntax:

b = a[1, :, None]

Then b has the required shape of (10, 1). Note that None is the same as np.newaxis and inserts a new axis of length 1.

(This is the same thing as writing b = a[1, :][:, None] but uses only one indexing operation, hence saves a few microseconds.)

If you want to continue using reshape (which is also fine for this purpose), it's worth remembering that you can use -1 for (at most) one axis to have NumPy figure out what the correct length should be instead:

b.reshape(-1, 1)
Sign up to request clarification or add additional context in comments.

1 Comment

I would add that .reshape is not an inplace operation, so it should be either b = b.reshape(-1, 1) or the inplace version b.shape = (-1, 1).
21

Use np.newaxis:

In [139]: b.shape
Out[139]: (10,)

In [140]: b=b[:,np.newaxis]

In [142]: b.shape
Out[142]: (10, 1)

4 Comments

Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem.
@SuperBiasedMan - the OP isn't asking for understanding, just a more concise expressions. I'm not sure it can be explained any more the OP's reshape version can. It's basic use of the np.newaxis variable.
@SuperBiasedMan I agree, code-only answers usually won't help. But as hpaulj said: there's not much explaining available here, the three commands above are self-explanatory. (Take a look at my other answers, I always try to explain what my answer does if it seems necessary).
I know there is no such obligation, but I would still appreciate a comment on the nature of the downvote. (Unless it was SuperBiasedMan, then I know the comment)
2

I think clearest way of doing this is by using np.expand_dims, which basically adds an axis to the array. If you use axis=-1, a new axis will be added as the last dimension.

b = np.expand_dims(b, axis=-1)

or if you want to me more concise:

b = np.expand_dims(b, -1)

Comments

0

Although the question is old, still it is worth to answer I think.

Use this style:

b = a[1:2, :]

2 Comments

This will not have shape (10, 1).
No but it will have shape (1,10) and b = a[:,1:2] will have shape (10,1)
-2

you can use np.asmatrix(b) as well

a.shape                 #--> (12,)
np.asmatrix(a).shape    #--> (1, 12)
np.asmatrix(a).T.shape  #--> (12, 1)

1 Comment

it returns matrix and according to this: stackoverflow.com/a/61156350 matrices will be deprecated in the future.

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.