Home » Flattening Arrays in NumPy tutorial with Examples

Flattening Arrays in NumPy tutorial with Examples

Java SE 11 Programmer II [1Z0-816] Practice Tests
Java SE 11 Programmer I [1Z0-815] Practice Tests
Oracle Java Certification
1 Year Subscription
Java SE 11 Developer (Upgrade) [1Z0-817]
Spring Framework Basics Video Course

Flattening an array in NumPy means converting a multi-dimensional array into a one-dimensional array.

This is commonly used when reshaping data, especially in machine learning, image processing, and data analysis, where you often need to format data into a single row or column.

In this tutorial, we will cover:

  1. Basic Flattening with flatten()
  2. Flattening with ravel()
  3. Using reshape() to Flatten Arrays
  4. Difference Between flatten() and ravel()
  5. Flattening Higher-Dimensional Arrays

Let’s go through each method with examples.

1. Basic Flattening with flatten()

The flatten() method in NumPy converts an array into a one-dimensional array.

It returns a new copy of the array in a flattened form.

Example 1: Flattening a 2D Array

import numpy as np

# Create a 2D array
array_2d = np.array([[1, 2, 3], [4, 5, 6]])

# Flatten the 2D array
flattened_array = array_2d.flatten()
print("Flattened Array:\n", flattened_array)

Output:

Flattened Array:
 [1 2 3 4 5 6]
  • Explanation: flatten() creates a new 1D array from the 2D array, copying the data in a row-major (C-style) order by default.

Example 2: Flattening in Column-Major Order

You can specify the order in which to flatten: ‘C' (row-major), ‘F' (column-major), or ‘A' (depends on memory layout).

# Flatten in column-major (Fortran-style) order
flattened_array_fortran = array_2d.flatten(order='F')
print("\nFlattened Array (Column-Major Order):\n", flattened_array_fortran)

Output:

Flattened Array (Column-Major Order):
 [1 4 2 5 3 6]
  • Explanation: With order='F', the array is flattened by columns, reading elements in a column-first order.

2. Flattening with ravel()

The ravel() function is similar to flatten(), but it returns a flattened view of the array if possible.

This means that ravel() often does not create a new array but instead provides a reference to the original array, making it more memory-efficient.

Example 3: Flattening a 2D Array with ravel()

# Flatten the array with ravel
raveled_array = array_2d.ravel()
print("Raveled Array:\n", raveled_array)

Output:

Raveled Array:
 [1 2 3 4 5 6]
  • Explanation: ravel() provides a 1D view of array_2d, which is generally more memory-efficient than flatten().

Example 4: Column-Major Order with ravel()

# Flatten with column-major (Fortran-style) order
raveled_array_fortran = array_2d.ravel(order='F')
print("\nRaveled Array (Column-Major Order):\n", raveled_array_fortran)

Output:

Raveled Array (Column-Major Order):
 [1 4 2 5 3 6]
  • Explanation: Like flatten(), ravel() also allows specifying order='F' to flatten the array by columns.

3. Using reshape() to Flatten Arrays

The reshape() function can also be used to flatten an array by setting its shape to (-1,), where -1 instructs NumPy to infer the dimension.

Example 5: Flattening a 2D Array with reshape()

# Flatten the array using reshape
reshaped_array = array_2d.reshape(-1)
print("Flattened Array using reshape:\n", reshaped_array)

Output:

Flattened Array using reshape:
 [1 2 3 4 5 6]
  • Explanation: reshape(-1) flattens the array into a 1D array by inferring the length of the dimension.

Example 6: Column-Major Order with reshape()

# Flatten the array using reshape in column-major order
reshaped_array_fortran = array_2d.reshape(-1, order='F')
print("\nFlattened Array using reshape (Column-Major Order):\n", reshaped_array_fortran)

Output:

Flattened Array using reshape (Column-Major Order):
 [1 4 2 5 3 6]
  • Explanation: reshape(-1, order='F') reshapes the array into a flattened 1D array in column-major order.

4. Difference Between flatten() and ravel()

While flatten() and ravel() may appear similar, there is an important difference between them:

  • flatten() always returns a copy of the array, which means modifications to the flattened array do not affect the original array.
  • ravel() returns a view of the array when possible, which is more memory-efficient. Modifying the output of ravel() may affect the original array.

Example 7: Modifying a Flattened Array with flatten() and ravel()

# Modify the flattened copy with flatten
flattened_copy = array_2d.flatten()
flattened_copy[0] = 99
print("Original Array after flatten modification:\n", array_2d)

# Modify the raveled view with ravel
raveled_view = array_2d.ravel()
raveled_view[0] = 99
print("\nOriginal Array after ravel modification:\n", array_2d)

Output:

Original Array after flatten modification:
 [[ 1  2  3]
 [ 4  5  6]]

Original Array after ravel modification:
 [[99  2  3]
 [ 4  5  6]]
  • Explanation: Changing flattened_copy (from flatten()) does not affect array_2d, while modifying raveled_view (from ravel()) changes the original array since ravel() returns a view when possible.

5. Flattening Higher-Dimensional Arrays

The same methods (flatten(), ravel(), and reshape()) can be used to flatten arrays of any dimensionality into a 1D array.

Example 8: Flattening a 3D Array

# Create a 3D array
array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

# Flatten the 3D array
flattened_3d = array_3d.flatten()
print("Flattened 3D Array:\n", flattened_3d)

Output:

Flattened 3D Array:
 [1 2 3 4 5 6 7 8]

Example 9: Column-Major Order Flattening of a 3D Array

# Flatten the 3D array in column-major order
flattened_3d_fortran = array_3d.flatten(order='F')
print("\nFlattened 3D Array (Column-Major Order):\n", flattened_3d_fortran)

Output:

Flattened 3D Array (Column-Major Order):
 [1 5 3 7 2 6 4 8]
  • Explanation: Flattening higher-dimensional arrays in column-major order changes the order in which elements are accessed and listed in the flattened array.

Summary of Array Flattening Techniques in NumPy

Function Description
flatten() Returns a flattened copy of the array; by default, flattens in row-major order (order='C').
ravel() Returns a flattened view of the array when possible; more memory-efficient than flatten().
reshape(-1) Converts any array into a 1D array by inferring the dimension.
Order (C vs F) Specifies the flattening order: row-major (C), column-major (F), or automatic (A).

Conclusion

In this tutorial, we explored various ways to flatten arrays in NumPy, including:

  • Flattening with flatten(), which returns a new flattened array.
  • Flattening with ravel(), which returns a flattened view of the original array when possible.
  • Flattening with reshape(), which converts an array to 1D with shape (-1,).
  • Column-major order flattening to control the order of elements in the flattened array.

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More