Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I flatten an 2d numpy array, which has different length in the second axis?

I have a numpy array which looks like:

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

But I can not flatten it,

In: myArray.flatten()
Out: array([[1, 2], [3]], dtype=object)

If I change the array to the same length in the second axis, then I can flatten it.

In: myArray2 = np.array([[1,2],[3,4]])
In: myArray2.flatten()
Out: array([1, 2, 3, 4])

My Question is:

Can I use some thing like myArray.flatten() regardless the dimension of the array and the length of its elements, and get the output: array([1,2,3])?

like image 902
xirururu Avatar asked Apr 08 '15 14:04

xirururu


People also ask

How to flatten a 2D NumPy array into 1D array in Python?

Flatten a 2d numpy array into 1d array in Python 1 With flatten. The flatten function in numpy is a direct way to convert the 2d array in to a 1D array. 2 Example. 3 Output. 4 With ravel. There is another function called ravel which will do a similar thing of flattening the 2D array into 1D. 5 Example. 6 Output. More ...

What is NumPy flatten() method?

What is Numpy Flatten () method? In python, there are many ways to re-structure the array according to the need of the person. But, there are some cases when we need a one-dimensional array rather than two- dimensional array.

How to convert a two-dimensional array to one-dimensional in NumPy?

This type of problem numpy library provides a function by which we can convert a two-dimensional array into a one-dimensional array, i.e., numpy.ndarray.flatten (). What is Numpy Flatten () method? Numpy.ndarray.flatten () is used when we need to return the copy of the array in a 1-d array rather than a 2-d or multi-dimensional array.

How to create a 2D array in Python?

To create a 2D array and syntax for the same is given below - arr = np.array([[1,2,3],[4,5,6]]) print(arr) [[1 2 3] [4 5 6]] Various functions on Array. Get shape of an array. arr.shape (2, 3) Get Datatype of elements in array. arr.dtype dtype('int64') Accessing/Indexing specific element. To get a specific element from an array use arr[r,c]


1 Answers

myArray is a 1-dimensional array of objects. Your list objects will simply remain in the same order with flatten() or ravel(). You can use hstack to stack the arrays in sequence horizontally:

>>> np.hstack(myArray)
array([1, 2, 3])

Note that this is basically equivalent to using concatenate with an axis of 1 (this should make sense intuitively):

>>> np.concatenate(myArray, axis=1)
array([1, 2, 3])

If you don't have this issue however and can merge the items, it is always preferable to use flatten() or ravel() for performance:

In [1]: u = timeit.Timer('np.hstack(np.array([[1,2],[3,4]]))'\
   ....: , setup = 'import numpy as np')
In [2]: print u.timeit()
11.0124390125

In [3]: u = timeit.Timer('np.array([[1,2],[3,4]]).flatten()'\
   ....: , setup = 'import numpy as np')
In [4]: print u.timeit()
3.05757689476

Iluengo's answer also has you covered for further information as to why you cannot use flatten() or ravel() given your array type.

like image 176
miradulo Avatar answered Oct 20 '22 06:10

miradulo