Numpy: get 1D array as a 2D array without changing

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

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

which gives

 #array.shape = (5,) 

but I would like the form to be recognized as a 2d array, for example.

 #array.shape = (5,1) 

So hstack can combine them. My current solution:

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

So I was wondering if there is a better way to do this? Will it

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

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

+5
source share
3 answers

Check the hstack and vstack . One or both of them pass arguments through atleast_nd . This is a perfectly acceptable way to modify 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 convert 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 do not know in advance which arrays are 1d, then column_stack easiest to use. Others require a small function that checks the dimension before applying the change.

Numpy: use reshape or newaxis to add dimensions

+5
source

If I understand your intent correctly, you want to convert an array of form (N) to an array of form (N, 1) so you can apply np.hstack :

 In [147]: np.hstack([np.atleast_2d([1,2,3,4,5]).T, np.atleast_2d([1,2,3,4,5]).T]) Out[147]: array([[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]]) 

In this case, you can use without redoing arrays and instead of np.column_stack :

 In [151]: np.column_stack([[1,2,3,4,5], [1,2,3,4,5]]) Out[151]: array([[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]]) 
+1
source

Just to add information on hpaulj answer. I was curious how quickly the four methods were described. The winner is the method of adding a column at the end of the 1d array.

Here is what I ran:

 import numpy as np import timeit v = [1,2,3,4,5] print('atleast2d:',timeit.timeit(lambda:np.atleast_2d(v).T)) print('reshape:',timeit.timeit(lambda:np.array(v).reshape(-1,1))) # saves the use of len() print('v[:,None]:', timeit.timeit(lambda:np.array(v)[:,None])) # adds a new dim at end print('np.array(v,ndmin=2).T:', timeit.timeit(lambda:np.array(v,ndmin=2).T)) # used by column_stack 

And the results:

 atleast2d: 4.455070924214851 reshape: 2.0535152913971615 v[:,None]: 1.8387219828073285 np.array(v,ndmin=2).T: 3.1735243063353664 
0
source

Source: https://habr.com/ru/post/1213482/


All Articles