Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use '.flat', '.flatiter' or '.flatten()'

When is it appropriate to use which of the three flattening arguments '.flat'/'.flatiter'/'.flatten'? I know that '.flat' returns a 1D iterator over an array, does this mean that the array is left in the original shape and each element in the array can be accessed with a single index (for example using a single for loop even though the array may be highly dimensional). And '.flatten' returns a complete copy of the original array flattened out into a 1D array.

Which is less resource intensive?

like image 894
Chris Church Avatar asked May 29 '15 06:05

Chris Church


People also ask

What is the difference between Ravel and flatten?

Ravel is faster than flatten() as it does not occupy any memory. Flatten() is comparatively slower than ravel() as it occupies memory. Ravel is a library-level function. Flatten is a method of an ndarray object.

What does flatten () do in numpy?

ndarray. flatten. Return a copy of the array collapsed into one dimension.

What is the use of flatten function?

FLATTEN is a table function that takes a VARIANT, OBJECT, or ARRAY column and produces a lateral view (i.e. an inline view that contains correlation referring to other tables that precede it in the FROM clause). FLATTEN can be used to convert semi-structured data to a relational representation.

Is reshape same as flatten?

It is essential because when we reshape an array, the reshape function is first going to flatten the input, and then split it into new arrays. Flattened means that we get rid of all squared brackets and return the array elements one by one enclosed in a single array.


1 Answers

flatiter is just the type of the iterator object returned by flat (docs). So all you need to know about it is that it's an iterator like any other.

Obviously, flatten consumes more memory and cpu, as it creates a new array, while flat only creates the iterator object, which is super fast.

If all you need is to iterate over the array, in a flat manner, use flat.

If you need an actual flat array (for purposes other than just explicitly iterating over it), use flatten.

like image 132
shx2 Avatar answered Sep 30 '22 01:09

shx2