HOWTO · NumPy
How to Convert 3D Array to 2D Array in Python
Learn how to convert a 3D array to a 2D array in Python using the reshape() function from the NumPy library. This article provides practical examples and detailed explanations to help you manipulate multi-dimensional arrays efficiently. Discover the importance of shape compatibility and element order for seamless data transformation. Perfect for data scientists and programmers looking to enhance their array manipulation skills.
On this page
Use NumPy’s numpy.reshape() operation to convert a 3D array to a 2D array while preserving its values. Choose a 2D shape whose dimensions multiply to the same number of elements as the input. For an array shaped (4, 2, 2), reshaping to (4, 4) keeps each first-axis group in one row and combines the other two axes into its columns. The right grouping depends on how your data should be interpreted.
Understanding 3D Array Shapes
A 3D NumPy array has three axes, and arr.shape reports the length of each axis as a tuple. The axes do not inherently mean depth, height, and width; their meaning comes from the data. For example, an image array might use height, width, and color-channel axes, while a simulation may use time, row, and column axes. Before reshaping, decide which dimensions should become rows and which should be combined into columns. Reshaping changes the organization of the axes, not the values themselves.
Convert a 3D Array with NumPy reshape()
Call reshape() with a tuple containing the desired number of rows and columns. This example creates values from 0 through 15, arranges them into a (4, 2, 2) array, and groups each pair of trailing axes into four columns.
Run this example:
import numpy as np
arr = np.arange(16).reshape(4, 2, 2)
arr_2d = arr.reshape(4, 4)
print("Input shape:", arr.shape)
print("Output shape:", arr_2d.shape)
print(arr_2d)
Input shape: (4, 2, 2)
Output shape: (4, 4)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
The target shape (4, 4) represents four rows and four columns. NumPy retains the values in the selected indexing order, so the new rows group the original values according to the chosen axes. Use a different target shape when your task calls for a different grouping; there is no single 2D layout that is right for every 3D dataset.
Derive the 2D Shape from the Input Array
When dimensions vary, read them from arr.shape instead of hard-coding them. If the input shape is (a, b, c) and each first-axis slice should become one row, the output shape is (a, b * c). In code, set rows, height, width = arr.shape and use arr.reshape(rows, height * width). This keeps the first axis intact and combines the second and third axes. If your intended rows correspond to a different axis, arrange or transpose the axes first, then choose the output shape to match that interpretation.
You can also ask NumPy to infer one dimension by writing -1 for it, such as arr.reshape(arr.shape[0], -1). NumPy calculates that dimension from the element count and the other specified dimensions. Use only one inferred dimension so the target shape is unambiguous.
Element Order: C and Fortran Styles
By default, reshape() uses C-style indexing order. This means NumPy visits the last axis fastest when reading values from the input and placing them into the result. Pass order='F' to use Fortran-style indexing order, where the first axis varies fastest. The order argument controls the indexing sequence used for the reshape; it does not by itself promise a particular physical memory layout. Most conversions can use the default. Choose Fortran order only when the sequence of values in the result should follow that convention.
Shape Compatibility and Common Errors
The product of the input dimensions must equal the product of the requested output dimensions. A (4, 2, 2) input has 16 elements, so (4, 4) or (2, 8) can hold the same values, while (4, 5) cannot. If the requested shape has an incompatible element count, NumPy raises ValueError. Check arr.size or calculate the dimension products when diagnosing this error. Also check that the chosen row and column grouping matches the meaning of your data: a shape can be mathematically compatible and still arrange values in a way that is unsuitable for the next operation.
For the function’s parameters and behavior, see the official NumPy reshape API reference.