Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flatten numpy array

Tags:

python

numpy

Is there a simple way in NumPy to flatten type object array?

I know .flatten() method flattens non-object type arrays constructed from same size arrays:

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

I2 a.flatten()
O2 array([1, 2, 3])

however, I can't get dtype=object array flattened:

I4 b
O4 array([[1], [2, 3], [3]], dtype=object)

I5 b.flatten()
O5 array([[1], [2, 3], [3]], dtype=object)

Thanks.

like image 731
Gökhan Sever Avatar asked Jul 06 '12 17:07

Gökhan Sever


People also ask

How do I flatten a numpy array?

The flatten() function is used to get a copy of an given array collapsed into one dimension. 'C' means to flatten in row-major (C-style) order. 'F' means to flatten in column-major (Fortran- style) order. 'A' means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise.

What is flatten () Python?

flatten() function return a copy of the array collapsed into one dimension. Syntax : numpy.ndarray.flatten(order='C') Parameters : order : [{'C', 'F', 'A', 'K'}, optional] 'C' means to flatten in row-major (C-style) order.

What is a flattened array?

Flattening an array is a process of reducing the dimensionality of an array. In other words, it a process of reducing the number of dimensions of an array to a lower number.

What is the flatter method?

Prototype - flatten() Method This method returns a flat (one-dimensional) version of the array. Nested arrays are recursively injected inline. This can prove very useful when handling the results of a recursive collection algorithm.


1 Answers

if you want [1,2,3,3], try this then

np.hstack(b)
like image 70
nye17 Avatar answered Oct 23 '22 16:10

nye17