| by Arround The Web | No comments

## NumPy np.flatten()

The NumPy package provides a flatten() function that allows you to return a copy of an array collapsed into a one-dimension array.

Let us explore.

## Function Syntax

The function has an elementary syntax as shown below:

ndarray.flatten(order='C')

## Function Parameters

The function takes only one parameter. The order parameter is used to define under which order the array is flattened.

The order parameter takes the following values:

1. ‘C’ – tells the function to flatten the array in row-major order.
2. ‘F’ – flatten the array in column-major order.
3. ‘A’ – flatten the array in row-order if the array is Fortran contiguous and column-order if otherwise.
4. ‘K’ – flatten the array in the order of elements (in memory).

By default, the function will sort the input array in row-major order.

## Return Value

The function will then return a copy of the input array but flattened into 1D.

## Example

To illustrate how the function works, consider the example shown below:

# import numpy
import numpy as np
arr = np.array([[12,32,6], [3,45,23]])
flattened = arr.flatten()
print(f"original: {arr}")
print(f"flattened: {flattened}")

The code above takes a 2D array and flattens it into a 1D array, as shown in the output below:

original: [[12 32  6]
[ 3 45 23]]
flattened: [12 32  6  3 45 23]

## Example 2

We can also apply the same operation to a multi-dimensional array. The code is as illustrated below:

arr_3d = np.array([[1,2,3], [0,4,5,], [9,4,6]])
flat = arr_3d.flatten()
print(f"original: {arr_3d}")
print(f"flattened: {flat}")

This should return:

original: [[1 2 3]
[0 4 5]
[9 4 6]]
flattened: [1 2 3 0 4 5 9 4 6]

## Example 3

Let us see what happens when we change the order parameter to ‘F’. Consider the code shown below:

arr = np.array([[12,32,6], [3,45,23]])
flattened = arr.flatten(order='F')
print(f"original: {arr}")
print(f"flattened: {flattened}")

This example tells the function to order the array via column-major order. The resulting array is as shown:

original: [[12 32  6]
[ 3 45 23]]
flattened: [12  3 32 45  6 23]

## Example 4

Ordering via the ‘A’ parameter returns an array as shown:

arr = np.array([[12,32,6], [3,45,23]])
flattened = arr.flatten('A')
print(f"original: {arr}")
print(f"flattened: {flattened}")

The order ‘A’ acts as a ‘smart’ option that selects the sort based on the array type. The function will flatten the array in row-major order in the example above.

original: [[12 32  6]
[ 3 45 23]]
flattened: [12 32  6  3 45 23]

## Example 5

The ‘K’ parameters returns an array as shown:

arr = np.array([[12,32,6], [3,45,23]])
flattened = arr.flatten('K')
print(f"original: {arr}")
print(f"flattened: {flattened}")

Output:

original: [[12 32  6]
[ 3 45 23]]
flattened: [12 32  6  3 45 23]

## Conclusion

In the article, we took a look at the flatten function in NumPy to flatten an input array into one dimension. With the help of examples, we illustrated how the function behaves under different order parameters.

See you at the next one!!!

Source: linuxhint.com