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.
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.
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.
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.
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.
if you want [1,2,3,3], try this then
np.hstack(b)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With