6

How to convert (5,) numpy array to (5,1)?

And how to convert backwards from (5,1) to (5,)?

What is the purpose of (5,) array, why is one dimension omitted? I mean why we didn't always use (5,1) form?

Does this happen only with 1D and 2D arrays or does it happen across 3D arrays, like can (2,3,) array exist?

UPDATE:

I managed to convert from (5,) to (5,1) by

a= np.reshape(a, (a.shape[0], 1)) 

but suggested variant looks simpler:

a = a[:, None] or a = a[:, np.newaxis]

To convert from (5,1) to (5,) np.ravel can be used

a= np.ravel(a)
7
  • 1
    .. if those are meant to be answers, why are they comments? Commented Oct 26, 2015 at 15:57
  • There's no need for "placeholder" comments, and it can often take more than two minutes to find a good dup target anyway. Commented Oct 26, 2015 at 16:04
  • 2
    Why should there be a 2nd dimension? Are you a MATLAB refugee? :) Commented Oct 26, 2015 at 16:06
  • @hpaulj Sometimes sklearn requires a 2 dimensional array of features Commented Oct 26, 2015 at 16:08
  • 2
    I don't think the duplicate suggestion is good, because none of the answers it suggests will work in this case. Commented Oct 26, 2015 at 16:24

3 Answers 3

6

A numpy array with shape (5,) is a 1 dimensional array while one with shape (5,1) is a 2 dimensional array. The difference is subtle, but can alter some computations in a major way. One has to be specially careful since these changes can be bull-dozes over by operations which flatten all dimensions, like np.mean or np.sum.

In addition to @m-massias's answer, consider the following as an example:

17:00:25 [2]: import numpy as np
17:00:31 [3]: a = np.array([1,2])
17:00:34 [4]: b = np.array([[1,2], [3,4]])
17:00:45 [6]: b * a
      Out[6]: 
array([[1, 4],
       [3, 8]])
17:00:50 [7]: b * a[:,None] # Different result!
      Out[7]: 
array([[1, 2],
       [6, 8]])

a has shape (2,) and it is broadcast over the second dimension. So the result you get is that each row (the first dimension) is multiplied by the vector:

17:02:44 [10]: b * np.array([[1, 2], [1, 2]])
      Out[10]: 
array([[1, 4],
       [3, 8]])

On the other hand, a[:,None] has the shape (2,1) and so the orientation of the vector is known to be a column. Hence, the result you get is from the following operation (where each column is multiplied by a):

17:03:39 [11]: b * np.array([[1, 1], [2, 2]])
      Out[11]: 
array([[1, 2],
       [6, 8]])

I hope that sheds some light on how the two arrays will behave differently.

Sign up to request clarification or add additional context in comments.

Comments

4

You can add a new axis to an array a by doing a = a[:, None] or a = a[:, np.newaxis]

As far as "one dimension omitted", I don't really understand your question, because it has no end : the array could be (5, 1, 1), etc.

Comments

4

Use reshape() function e.g. open python terminal and type following:

    >>> import numpy as np
    >>> a = np.random.random(5)
    >>> a
    array([0.85694461, 0.37774476, 0.56348081, 0.02972139, 0.23453958])
    >>> a.shape
    (5,)
    >>> b = a.reshape(5, 1)
    >>> b.shape
    (5, 1)

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.