Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 756
mrgloom Avatar asked Oct 26 '15 15:10

mrgloom


3 Answers

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.

like image 80
musically_ut Avatar answered Oct 12 '22 03:10

musically_ut


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.

like image 34
P. Camilleri Avatar answered Oct 12 '22 05:10

P. Camilleri


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)
like image 23
Himanshu Singh Avatar answered Oct 12 '22 04:10

Himanshu Singh