.flatten()
The .flatten()
method converts a multi-dimensional NumPy array into a one-dimensional array. This method creates a copy of the original array with all elements arranged in a single dimension while preserving the values and their order. The flattening process essentially “unrolls” the array, maintaining the elements in a sequential arrangement based on the specified order parameter.
The .flatten()
method is commonly used in data preparation for machine learning algorithms, simplifying matrix operations, and restructuring data for visualization. It’s particularly useful when transforming complex multi-dimensional data structures into a linear format for analysis or when preparing inputs for algorithms that require one-dimensional arrays.
Syntax
ndarray.flatten(order='C')
Parameters:
order
(Optional): Specifies the order in which the elements of the multi-dimensional array are read during flattening. Possible values include:'C'
: Row-major (C-style) order. Elements are read in row-by-row (default)'F'
: Column-major (Fortran-style) order. Elements are read in column-by-column'A'
: Uses Fortran-style order if the array is Fortran contiguous in memory, otherwise uses C-style order'K'
: Elements are read in the order they occur in memory
Return value:
Returns a new one-dimensional ndarray containing a copy of the flattened elements of the original array.
Example 1: Converting two-dimensional array into a one-dimensional array
This example demonstrates how to convert a basic two-dimensional array into a one-dimensional array with .flatten()
:
import numpy as np# Create a 2D arrayarray_2d = np.array([[1, 2], [3, 4], [5, 6]])print("Original array:")print(array_2d)print("Shape:", array_2d.shape)# Flatten the arrayflattened_array = array_2d.flatten()print("\nFlattened array:")print(flattened_array)print("Shape:", flattened_array.shape)
The output produced by this code is:
Original array:[[1 2][3 4][5 6]]Shape: (3, 2)Flattened array:[1 2 3 4 5 6]Shape: (6,)
This code creates a 3×2 two-dimensional array and flattens it to a one-dimensional array with six elements. The .flatten()
method preserves the order of elements, reading them row by row(C-style order by default).
Example 2: Flattening with Different Order Parameters
This example shows how the order
parameter affects the arrangement of elements in the flattened array, which can be important when working with multi-dimensional data that needs to be processed in a specific order:
import numpy as np# Create a 2D arrayarray_2d = np.array([[1, 2, 3], [4, 5, 6]])print("Original array:")print(array_2d)# Flatten with different order parametersc_style = array_2d.flatten(order='C')f_style = array_2d.flatten(order='F')k_style = array_2d.flatten(order='K')print("\nC-style (row-major) flattening:")print(c_style)print("\nF-style (column-major) flattening:")print(f_style)print("\nK-style (memory order) flattening:")print(k_style)
The code generates the following output:
Original array:[[1 2 3][4 5 6]]C-style (row-major) flattening:[1 2 3 4 5 6]F-style (column-major) flattening:[1 4 2 5 3 6]K-style (memory order) flattening:[1 2 3 4 5 6]
This example illustrates how different order parameters affect the sequence of elements in the flattened array. C-style reads row-by-row, while F-style reads column-by-column, producing different results from the same original array.
Codebyte Example: Processing Image Data with Flatten
This example demonstrates a common real-world application of .flatten()
when processing image data for machine learning:
This example shows how image data (represented as a 2D array) can be flattened for processing and then reshaped back to its original dimensions. This pattern is common in image processing and machine learning workflows where algorithms may require 1D input vectors.
Frequently Asked Questions
1. What's the difference between `.flatten()` and `.ravel()`?
The main difference is that `.flatten()` always returns a copy of the array, while `.ravel()` returns a view of the original array when possible (which is more memory-efficient). If modifications to the flattened array should not affect the original, use `.flatten()`.
2. How does `.flatten()` handle arrays with more than two dimensions?
`.flatten()` works the same way regardless of the number of dimensions. It converts an array of any dimensionality into a single one-dimensional array, preserving the elements according to the specified order.
3. Can I specify the data type of the flattened array?
`.flatten()` preserves the data type of the original array. To change the data type, you can use the [`.astype()`](https://www.codecademy.com/resources/docs/numpy/ndarray/astype) method on the flattened result, for example: `array.flatten().astype(np.float64)`.
4. Does `.flatten()` work with masked arrays?
Yes, for masked arrays (from `numpy.ma`), `.flatten()` preserves the mask in the flattened array, so the masked elements remain masked in the result.
5. How does `.flatten()` affect performance in numerical computations?
Since `.flatten()` creates a copy, it has memory and computational overhead. For large arrays or performance-critical code, consider if alternatives like `.ravel()` (which returns a view when possible) or direct operations on the multi-dimensional array would be more efficient.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours