
Understanding numpy.flatten(): How It Works in Python
When working with multidimensional arrays in Python, we often need to convert them into one-dimensional arrays. NumPy provides several methods to achieve this, and one of the most commonly used is the flatten()
function. Let’s dive deep into how numpy.flatten()
works and when you should use it.
What Is numpy.flatten()
?
The flatten()
function in NumPy is a method of ndarray objects that returns a copy of the array collapsed into one dimension. This means it takes a multi-dimensional array and converts it into a continuous flat array.
Basic Syntax
The syntax for using flatten()
is straightforward:
array.flatten(order='C')
Where:
- order (optional) – Specifies the order in which elements are read from the array. Default is ‘C’.
Parameters Explanation
The order
parameter defines the reading direction:
Order | Description |
---|---|
'C' |
Row-major order (C-like). Reads row by row. |
'F' |
Column-major order (Fortran-like). Reads column by column. |
'A' |
Chooses between ‘C’ or ‘F’ automatically, depending on memory layout. |
'K' |
Preserves the order as stored in memory. |
Best Example of numpy.flatten()
Let’s take a look at a concrete example to understand how numpy.flatten()
works:
import numpy as np
# Creating a 2D NumPy array
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Flattening the array in row-major order
flat_arr = arr.flatten()
print(flat_arr)
**Output:**
[1 2 3 4 5 6]
As you can see, the 2D array has been converted into a 1D array.
Different Orders in Action
Now, let’s explore how the order parameter affects the flattening process:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Flatten using Fortran-style (column-by-column)
flat_F = arr.flatten(order='F')
print(flat_F)
**Output:**
[1 4 2 5 3 6]
In this case, values are taken column by column instead of row by row.
Key Differences Between flatten()
and ravel()
NumPy provides another method called ravel()
, which seems similar to flatten()
, but there are some key distinctions:
flatten()
always returns a new copy.ravel()
returns a flattened view if possible (thus avoiding copying when unnecessary).
Example comparison:
arr = np.array([[1, 2, 3], [4, 5, 6]])
flat_copy = arr.flatten() # Always a new copy
flat_view = arr.ravel() # A view when possible
When to Use numpy.flatten()
?
Use flatten()
when:
- You need a completely new copy of the array.
- You want to ensure modifications to the flattened array don’t affect the original array.
- You are not concerned with memory efficiency.
Use ravel()
when:
- You need a flattened view instead of a new copy.
- You want better memory efficiency.
- You are okay with modifications reflecting in the original array (if it is a view).
Conclusion
The numpy.flatten()
function is a simple yet powerful method to convert a multi-dimensional array into a one-dimensional array. It ensures a new copy is created, making it a safer choice when you do not want modifications affecting the original array. However, if efficiency is a concern, you might want to consider ravel()
instead.
Understanding when and how to use flatten()
effectively will help you manipulate data with ease in NumPy. I hope this article helped explain the best examples and use cases of numpy.flatten()
in Python!