Flatten to a 1D NumPy Array
To flatten any NumPy array to a one-dimensional array, use the array.flatten()
method that returns a new flattened 1D array.
Here’s a simple example:
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr.flatten()) # [1 2 3 4 5 6]
Flatten NumPy Array Along Axis with reshape()
To “flatten” a NumPy array along an axis, it’s often better to use the array.reshape()
function. You can pass the new shape
tuple as an argument.
Here’s an example:
>>> arr = np.array([[1, 2, 3], [4, 5, 6]]) >>> arr.reshape(2, 3) array([[1, 2, 3], [4, 5, 6]]) >>> arr.reshape(3, 2) array([[1, 2], [3, 4], [5, 6]]) >>> arr.reshape(6, -1) array([[1], [2], [3], [4], [5], [6]])
π‘ Recommended Tutorial: Reshaping a NumPy Array
There are more ways to flatten a NumPy array along an axis.
Flatten NumPy Array of Lists
To convert the list of arrays (in variable lst
) to a flat array, you can use any of the following functions:
np.concatenate(lst).ravel()
np.array(lst).ravel()
np.array(lst).flatten()
np.array(lst).reshape(-1)
The following shows the first approach—you can replace the highlighted line with any of the given approaches.
import numpy as np lst = [np.array([1, 2, 3]), np.array([4, 5, 6]), np.array([7, 8, 9])] print(lst) # [array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9])] # Convert the List of Array to a Flat Array arr = np.concatenate(lst).ravel() print(arr) # [1 2 3 4 5 6 7 8 9]
A great performance analysis was performed by SO user “ayorgo” that shows that reshape()
and ravel()
are much faster because they operate on a view of the original array rather than returning a copy like flatten()
:
Flatten NumPy Array of Arrays
To flatten a NumPy array of arrays, say arr
, use the np.concatenate(arr).ravel()
function call. The result will be a one-dimensional (1D) flattened NumPy array of values.
Here’s an example:
import numpy as np arr = np.array([np.array([1, 2, 3]), np.array([4, 5, 6]), np.array([7, 8, 9])]) print(arr) ''' [[1 2 3] [4 5 6] [7 8 9]] ''' # Convert the Array of Array to a Flat Array arr = np.concatenate(arr).ravel() print(arr) # [1 2 3 4 5 6 7 8 9]
Flatten NumPy Array of Tuples
To convert the tuple of arrays (in variable t
) to a flat array, you can use any of the following functions:
np.concatenate(t).ravel()
np.array(t).ravel()
np.array(t).flatten()
np.array(t).reshape(-1)
The following shows all those approaches and how they result in the same output:
import numpy as np t = (np.array([1, 2, 3]), np.array([4, 5, 6]), np.array([7, 8, 9])) print(t) # (array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9])) # Convert the Tuple of Arrays to a Flat Array print(np.concatenate(t).ravel()) print(np.array(t).ravel()) print(np.array(t).flatten()) print(np.array(t).reshape(-1)) # [1 2 3 4 5 6 7 8 9]
NumPy Flatten Array – Only Some Dimensions (Row, Column, etc.)
To flatten only some dimensions in a NumPy array, use the arr.reshape()
function and pass the shape tuple of the desired array. This way, you can flatten rows and columns easily.
For example, to flatten an array with shape (10, 20, 30)
, you can call array.reshape(200, 30)
that collapses (i.e., flattens) the first two dimensions into one.
import numpy as np arr = np.zeros((10, 20, 30)) flat = arr.reshape(200, 30) print(flat.shape) # (200, 30)
Where to Go From Here?
Thanks for reading through this whole tutorial. If you feel like you’re in need of some more NumPy education, check out our full 8000-word mega tutorial on the Finxter blog:
π Recommended Tutorial: NumPy — Everything You Need to Know to Get Started