In this article we are going to learn about the Numpy flatten() function for Array Shape Manipulation with code example and different orders.
What is NumPy flatten() function
Numpy library provides a function, flatten() which is used to convert an array from any shape to a 1-D array.The 1-D array is a linear array, the function name is flattened, which means it flattens the 2-D or N-D arrays to the 1-D linear array.
Syntax
ndarray.flatten(order='C')
Parameter
Order: This function accepts one argument. This argument is used to provide the order in which you want to flatten your array. It means you can control the order as per your program’s need. So let us understand this with examples and different order options that this function supports.
NumPy Program to flat using flatten() function
samplearray = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print("Original array",samplearray)
print(samplearray.shape)
print(samplearray.size)
flat_array = samplearray.flatten()
print("Array after flatten:",flat_array)
Output:
Original array [[1 2 3]
[4 5 6]
[7 8 9]]
(3, 3)
9
Array after flatten: [1 2 3 4 5 6 7 8 9]
Important Note:
The flatten() function operates on the array we have and provides us with another array in the form of a new 1-D array. An important point to note here is that the flatten function does not modify the original array so as we can see sample array is still of the same size and shape. Instead, we got another array in the form of a linear 1-D array.
We saw the default behavior of the flatten function when we didn’t pass any argument to it. Now let us understand the different flavors of flatten() function where we can pass the order argument and decide the output order.
1.Flatten NumPy array Row wise order ‘C’
Row-wise order simply means we want to flatten our array row-wise, which means we want to flatten the function to arrange our elements row-wise to create a linear 1-D array. Here C stands for C-like index order.Let us jump to our example:
samplearray = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print("Original array",samplearray)
flat_array = samplearray.flatten(order = 'C')
print("Array flatten row wise:",flat_array)
Output:
Array flatten row wise: [1 2 3 4 5 6 7 8 9]
2. Flatten NumPy array Column wise order ‘F’
Column wise order simply means we want to flatten our array column-wise, which means we want to flatten() the function to arrange our elements column-wise to create a linear 1-D array. Here F stands for Fortran-like index order. Let us jump to our example:
Python Numpy program to flatten array column-wise
samplearray = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print("Original array",samplearray)
flat_array = samplearray.flatten(order = 'F')
print("Array flatten Column wise:",flat_array)
Output:
Array flatten Column wise: [1 4 7 2 5 8 3 6 9]
3. Using memory order for ‘A’ Order
The third-order that the flatten function supports is Order ‘A’. Before we see the application of the flatten function by using third-order ‘A’, we must understand what is this order means.
As per NumPy library documentation:
‘A’ means to flatten in column-wise order if the array is Fortran contiguous in memory, row-major order otherwise. To understand this we must understand what is Fortran contiguous in-memory layout of an array. When we perform an operation on an array then there is a possibility the memory layout is different from the original array.
Let us understand this with our example: We have a sample array that has a memory layout as:
samplearray = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print("Original array",samplearray)
print(samplearray.flags)
Output:
Output:
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
If we take a transposed view of this array, and then check the memory layout again.
Transposed view of array checking memory layout
samplearray = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print("Original array",samplearray)
Transpose_view = samplearray.T
print(Transpose_view)
print(Transpose_view.flags)
Output:
[[1 4 7]
[2 5 8]
[3 6 9]]
C_CONTIGUOUS : False
F_CONTIGUOUS : True
OWNDATA : False
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
If we check now the transpose_view looks a different in-memory layout.Now if we call the flatten function on this view then we will get the flatten array column-wise based on the memory layout which is Fortran contiguous (F_CONTIGUOUS)
samplearray = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print("Original array",samplearray)
Transpose_view = samplearray.T
print("Array flatten A Order:",Transpose_view.flatten(order= 'A'))
Output:
Array flatten A Order: [1 2 3 4 5 6 7 8 9]
Similarly, if we do a flatten on samplearray , we will get flatten row-wise because it is C_CONTIGUOUS.
samplearray = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print("Original array",samplearray)
print("Array flatten A Order:",samplearray.flatten(order= 'A'))
Output:
Array flatten A Order: [1 2 3 4 5 6 7 8 9]
4. Using memory order for ‘K’ Order
The fourth and Final Order is ‘K’ Order. ‘K’ means to flatten the array in the order the elements occur in memory. The transpose view is different from the memory layout so both the results are the same
samplearray = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print("Original array",samplearray)
Transpose_view = samplearray.Tprint("Array flatten K Order:",Transpose_view.flatten(order= 'K'))
print("Array flatten K Order:",samplearray.flatten(order= 'K'))
Output:
Array flatten K Order: [1 2 3 4 5 6 7 8 9]
Array flatten K Order: [1 2 3 4 5 6 7 8 9]