# [Numpy-discussion] Change of behavior in flatten between 1.0.4 and 1.1

Pauli Virtanen pav@iki...
Tue Jul 1 16:38:50 CDT 2008

```Tue, 01 Jul 2008 17:18:55 -0400, Stuart Brorson wrote:

> Hi --
>
> I have noticed a change in the behavior of numpy.flatten(True) between
> NumPy 1.0.4 and NumPy 1.1.   The change affects 3D arrays.  I am
> wondering if this is a bug or a feature.
>
> Here's the change.  Note that the output from flatten(True) is different
> between 1.0.4 and 1.1.

http://scipy.org/scipy/numpy/ticket/676

The rationale was to make the output from .flatten(1) to be equal to
interpreting the data as it would appear in a multidimensional Fortran
array (equivalent to reshape(a, (prod(a.shape),), order='F'), IIRC). In
1.0.4 a.flatten(1) only swapped the two first axes and then flattened in
C-order. In 1.1, a.flatten(1) == a.transpose().flatten().

To me, it appeared that the behavior in 1.0.4 was incorrect, so I filed
the bug (after being bitten by it in real code...) and submitted a patch
that got applied.

--
Pauli Virtanen

> =======  First the preliminary set up:  =======
>
> In [3]: A = numpy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10],
> [11, 12]]])
>
> In [4]: A
>
> Out[4]:
>
> array([[[ 1,  2],
>          [ 3,  4]],
>
>         [[ 5,  6],
>          [ 7,  8]],
>
>         [[ 9, 10],
>          [11, 12]]])
>
> =======  Now the change:  Numpy 1.0.4  =======
>
> In [5]: A.flatten()
>
> Out[5]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])
>
> In [6]: A.flatten(True)
>
> Out[6]: array([ 1,  5,  9,  2,  6, 10,  3,  7, 11,  4,  8, 12])

Here the two first dimensions are swapped and data is interpreted in C-
order.

> =======  Numpy 1.1  =======
>
> In [4]: A.flatten()
>
> Out[4]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])
>
> In [5]: A.flatten(True)
>
> Out[5]: array([ 1,  5,  9,  3,  7, 11,  2,  6, 10,  4,  8, 12])

Here dimensions are transposed, and data is interpreted in C-order.

--
Pauli Virtanen

```