Taaam - 2 years ago 855
Python Question

# Numpy: get 1D array as 2D array without reshape

I have need for hstacking multple arrays with with the same number of rows (although the number of rows is variable between uses) but different number of columns. However some of the arrays only have one column, eg.

``````array = np.array([1,2,3,4,5])
``````

which gives

``````#array.shape = (5,)
``````

but I'd like to have the shape recognized as a 2d array, eg.

``````#array.shape = (5,1)
``````

So that hstack can actually combine them.
My current solution is:

``````array = np.atleast_2d([1,2,3,4,5]).T
#array.shape = (5,1)
``````

So I was wondering, is there a better way to do this? Would

``````array = np.array([1,2,3,4,5]).reshape(len([1,2,3,4,5]), 1)
``````

be better?
Note that my use of [1,2,3,4,5] is just a toy list to make the example concrete. In practice it will be a much larger list passed into a function as an argument. Thanks!

Check the code of `hstack` and `vstack`. One, or both of those, pass the arguments through `atleast_nd`. That is a perfectly acceptable way of reshaping an array.

Some other ways:

``````arr = np.array([1,2,3,4,5]).reshape(-1,1)  # saves the use of len()
arr = np.array([1,2,3,4,5])[:,None]  # adds a new dim at end
np.array([1,2,3],ndmin=2).T  # used by column_stack
``````

`hstack` and `vstack` transform their inputs with:

``````arrs = [atleast_1d(_m) for _m in tup]
[atleast_2d(_m) for _m in tup]
``````

test data:

``````a1=np.arange(2)
a2=np.arange(10).reshape(2,5)
a3=np.arange(8).reshape(2,4)

np.hstack([a1.reshape(-1,1),a2,a3])
np.hstack([a1[:,None],a2,a3])
np.column_stack([a1,a2,a3])
``````

result:

``````array([[0, 0, 1, 2, 3, 4, 0, 1, 2, 3],
[1, 5, 6, 7, 8, 9, 4, 5, 6, 7]])
``````

If you don't know ahead of time which arrays are 1d, then `column_stack` is easiest to use. The others require a little function that tests for dimensionality before applying the reshaping.

Numpy: use reshape or newaxis to add dimensions

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download