Numpy Flatten: A step-by-step Guide Examples

NumPy, a fundamental library for scientific computing in Python, offers a plethora of powerful functions for array manipulation. Among these, the flatten() method stands out as a versatile tool for reshaping arrays. In this blog post, we'll embark on a journey to explore the numpy flatten() function step by step, unraveling its capabilities through insightful examples.

What is NumPy's flatten()?

Before delving into examples, let's grasp the essence of flatten(). This function is designed to transform multi-dimensional arrays into one-dimensional arrays, providing a flattened view of the original data. It returns a new array, leaving the original array unchanged.

Step-by-Step Examples:

Basic Flattening:

``````
import numpy as np

# Create a 2D array
original_array = np.array([[11, 62, 13], [44, 55, 62]])

# Flatten the array
flattened_array = original_array.flatten()

# Display results
print("Original Array:\n", original_array)
print("Flattened Array:", flattened_array)
``````

In this example, we create a 2D array and use flatten() to convert it into a 1D array. The resulting array retains the original order of elements.

Output:

``````
Original Array:
[[11 62 13]
[44 55 62]]

Flattened Array: [11 62 13 44 55 62]
``````

Order Parameter:

NumPy's flatten() function provides the order parameter, allowing you to control the order in which elements are flattened. The order parameter accepts four values: 'C' (C-style), 'F' (Fortran-style), 'A' (Any), and 'K' (Keep). Let's explore each of them step by step with examples:

C-Style (row-wise):

``````
import numpy as np

# Create a 2D array
original_array = np.array([[11, 22, 34], [45, 54, 65]])

# Flatten the array with 'C' order (row-wise)
flattened_array_c = original_array.flatten(order='C')

# Display results
print("Original Array:\n", original_array)
print("Flattened Array (C-Style):", flattened_array_c)
``````

In this example, the 'C' order flattens the array row-wise, meaning it concatenates the rows one after the other.

Output:

``````
Original Array:
[[11 22 34]
[45 54 65]]
Flattened Array (C-Style): [11 22 34 45 54 65]
``````

F-Style (column-wise):

The order parameter allows us to specify the order in which elements are flattened. Using 'F' (Fortran-style), we flatten the array column-wise.

Output:

``````
Original Array:
[[15 24 36]
[44 45 66]]

Flattened Array (Column-wise): [15 44 24 45 36 66]
``````

Any (A-Style):

``````
import numpy as np

# Create a 2D array
original_array = np.array([[61, 42, 53], [94, 65, 46]])

# Flatten the array with 'A' order (Any)
flattened_array_a = original_array.flatten(order='A')

# Display results
print("Original Array:\n", original_array)
print("Flattened Array (Any-Style):", flattened_array_a)
``````

The 'A' order, or Any order, allows NumPy to choose the most efficient order based on the memory layout of the array. It is a good choice when the order doesn't matter, and NumPy can decide the best strategy.

Output:

``````
Original Array:
[[61 42 53]
[94 65 46]]
Flattened Array (Any-Style): [61 42 53 94 65 46]
``````

Keep (K-Style):

``````
import numpy as np

# Create a 2D array
original_array = np.array([[51, 42, 53], [54, 55, 56]])

# Flatten the array with 'K' order (Keep)
flattened_array_k = original_array.flatten(order='K')

# Display results
print("Original Array:\n", original_array)
print("Flattened Array (Keep-Style):", flattened_array_k)
``````

The 'K' order, or Keep order, preserves the order of elements as they are stored in memory. This can be useful when the array is a result of a previous operation, and you want to maintain the same order.

``````
Original Array:
[[51 42 53]
[54 55 56]]
Flattened Array (Keep-Style): [51 42 53 54 55 56]
``````

Modifying Flattened Data:

``````
import numpy as np

# Create a 2D array
original_array = np.array([[11, 25, 36], [44, 55, 61]])

# Flatten the array
flattened_array = original_array.flatten()

# Modify a value in the flattened array
flattened_array[0] = 99

# Display results
print("Original Array:\n", original_array)
print("Modified Flattened Array:", flattened_array)
``````

Even though flatten() returns a new array, modifying elements in the flattened array affects the original array.

Output:

``````
Original Array:
[[11 25 36]
[44 55 61]]

Modified Flattened Array: [99 25 36 44 55 61]
``````

Reshape vs. Flatten:

``````
import numpy as np

# Create a 2x3 array
original_array = np.array([[102, 12, 13], [14, 15, 16]])

# Reshape the array to 1x6
reshaped_array = original_array.reshape(1, 6)

# Flatten the array
flattened_array = original_array.flatten()

# Display results
print("Original Array:\n", original_array)
print("Reshaped Array (1x6):\n", reshaped_array)
print("Flattened Array:", flattened_array)
``````

This example highlights the difference between reshape() and flatten(). While reshape() changes the shape of the array, flatten() converts it to a 1D array.

Output:

``````
Original Array:
[[102  12  13]
[ 14  15  16]]

Reshaped Array (1x6):
[[102  12  13  14  15  16]]

Flattened Array: [102  12  13  14  15  16]
``````

Understanding the order parameter in NumPy's flatten() function gives you the flexibility to control how your multi-dimensional arrays are flattened. Whether you need row-wise, column-wise, or an optimized approach, NumPy's flatten() with the order parameter has you covered. Choose the order that best suits your specific use case for efficient array manipulation.