# cf.Data.flatten¶

Data.flatten(axes=None, inplace=False)[source]

Flatten axes of the data

Any subset of the axes may be flattened.

The shape of the data may change, but the size will not.

The flattening is executed in row-major (C-style) order. For example, the array [[1, 2], [3, 4]] would be flattened across both dimensions to [1 2 3 4].

Parameters: axes: (sequence of) int or str, optional Select the axes. By default all axes are flattened. The axes argument may be one, or a sequence, of: An internal axis identifier. Selects this axis. An integer. Selects the axis coresponding to the given position in the list of axes of the data array. No axes are flattened if axes is an empty sequence. inplace: bool, optional If True then do the operation in-place and return None. Data or None The flattened data, or None if the operation was in-place.

Examples

>>> d = cf.Data(numpy.arange(24).reshape(1, 2, 3, 4))
>>> d
<CF Data(1, 2, 3, 4): [[[[0, ..., 23]]]]>
>>> print(d.array)
[[[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]]

>>> e = d.flatten()
>>> e
<CF Data(24): [0, ..., 23]>
>>> print(e.array)
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]

>>> e = d.flatten([])
>>> e
<CF Data(1, 2, 3, 4): [[[[0, ..., 23]]]]>

>>> e = d.flatten([1, 3])
>>> e
<CF Data(1, 8, 3): [[[0, ..., 23]]]>
>>> print(e.array)
[[[ 0  4  8]
[ 1  5  9]
[ 2  6 10]
[ 3  7 11]
[12 16 20]
[13 17 21]
[14 18 22]
[15 19 23]]]

>>> d.flatten([0, -1], inplace=True)
>>> d
<CF Data(4, 2, 3): [[[0, ..., 23]]]>
>>> print(d.array)
[[[ 0  4  8]
[12 16 20]]
[[ 1  5  9]
[13 17 21]]
[[ 2  6 10]
[14 18 22]]
[[ 3  7 11]
[15 19 23]]]