31

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?

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
Alexandr M.
  • 411
  • 1
  • 4
  • 4

5 Answers5

31

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)
Alex Riley
  • 169,130
  • 45
  • 262
  • 238
  • 3
    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)`. – Imanol Luengo Jan 03 '16 at 00:12
20

Use np.newaxis:

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

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

In [142]: b.shape
Out[142]: (10, 1)
  • 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 Nov 02 '15 at 16:12
  • @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. – hpaulj Nov 02 '15 at 16:45
  • 2
    @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). – Andras Deak -- Слава Україні Nov 02 '15 at 17:06
  • 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) – Andras Deak -- Слава Україні Nov 05 '15 at 10:28
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)
guiweb
  • 965
  • 6
  • 12
0

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

Use this style:

b = a[1:2, :]
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
-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)
  • 2
    it returns matrix and according to this: https://stackoverflow.com/a/61156350 matrices will be deprecated in the future. – iedmrc Nov 02 '20 at 14:32