cf.Data.flatten

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

Flatten specified 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].

New in version 3.0.2.

Parameters
axes: (sequence of) int

Select the axes to be flattened. By default all axes are flattened. Each axis is identified by its integer position. No axes are flattened if axes is an empty sequence.

inplace: bool, optional

If True then do the operation in-place and return None.

Returns
Data or None

The flattened data, or None if the operation was in-place.

Examples

>>> import numpy as np
>>> d = cf.Data(np.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]]]