Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python NumPy reshape, flatten, ravel, and transpose

Learn the differences between NumPy's reshape, flatten, ravel, and transpose, including when each returns a view versus a copy and how memory layout affects performance.

~5 min readPython
#NumPy#array manipulation#reshape#flatten
Diagram showing NumPy array transformations: reshape, flatten, ravel, and transpose with view and copy labels.

When working with NumPy arrays in Python, the functions reshape, flatten, ravel, and transpose all change how data is organized, but they differ in whether they return a view or a copy, how they handle memory layout, and what constraints they place on the input. Choosing the right one matters for both correctness and performance. This article explains the behavior of each function, the view-versus-copy distinction, and the practical criteria for selecting the right tool for a given task.

Reshape: Changing Shape Without Changing Data

reshape returns a new array with the same data but a different shape. The total number of elements must remain the same. For example:

python
import numpy as np a = np.arange(12) b = a.reshape(3, 4) print(b.shape) # (3, 4) print(b)

reshape attempts to return a view of the original array. A view shares the underlying data buffer, so when a view is returned, modifying b also modifies a. However, reshape does not always return a view. If the original array is non-contiguous or if the requested shape requires a different memory layout, NumPy will return a copy instead. The order parameter controls the index order: 'C' (row-major, default), 'F' (column-major), or 'A' (which uses Fortran-like order when the input is Fortran-contiguous and C-like order otherwise).

A common mistake is assuming reshape always returns a view. The safe approach is to check the base attribute or use np.shares_memory(a, b) to determine whether the result shares memory with the input.

Flatten: Always a Copy

flatten returns a one-dimensional copy of the array. It always copies the data, so the result is independent of the original array. By default, it uses C order, but you can specify order='F' for column-major flattening.

python
a = np.array([[1, 2], [3, 4]]) c = a.flatten() c[0] = 99 print(a) # unchanged print(c) # [99, 2, 3, 4]

Because flatten always copies, it is safe to modify the result without affecting the original. The downside is the memory overhead of duplicating the data, especially for large arrays. Use flatten when you need a new, independent one-dimensional array.

Ravel: A View When Possible

ravel also returns a one-dimensional array, but it returns a view of the original array when the memory layout allows it. If the array is contiguous in C order, ravel returns a view. If the array is non-contiguous, ravel may need to copy the data to produce a contiguous result.

python
a = np.arange(6).reshape(2, 3) r = a.ravel() r[0] = 100 print(a) # array([[100, 1, 2], [3, 4, 5]]) — view, so a changed

When the original array is already contiguous, ravel is faster and uses less memory than flatten because it avoids copying. However, if you later modify the raveled array, you will also modify the original. If you need a copy regardless, use flatten.

The order parameter in ravel works similarly to flatten, but the default is 'C' and it respects the array's layout when order='A' is used.

Transpose: Reversing Axes

transpose permutes the axes of an array. With no axes argument, it reverses the order of axes; with a tuple, it applies the requested permutation. For a 2D array, reversing the axes swaps rows and columns. transpose always returns a view of the original data—it never copies.

python
a = np.arange(6).reshape(2, 3) t = a.transpose() print(t.shape) # (3, 2) print(t) # [[0 3] # [1 4] # [2 5]]

Because transpose returns a view, it does not allocate a new data buffer. However, the resulting array is typically non-contiguous in memory. The data is stored in the original buffer, but the strides are rearranged so that the logical order differs from the physical order. This has performance implications: operations on a transposed array may be slower because accessing elements requires non-sequential memory access.

View vs Copy: Memory and Performance Implications

The view-versus-copy distinction is central to understanding these functions. A view shares the underlying data buffer with the original array; a copy allocates a new buffer and duplicates the data. Views are cheaper to create because they avoid copying, but they introduce aliasing: changes to the view affect the original and vice versa.

For performance, views can be beneficial when you only need to read data or when you want to avoid duplicating a large array. However, non-contiguous views, such as those produced by transpose, can lead to slower element access due to poor cache locality. If you plan to perform many operations on a transposed array, it may be worth calling np.ascontiguousarray to obtain a contiguous copy, especially if the operations are computationally intensive.

Copies, on the other hand, give you independent data and can be rearranged into a contiguous layout. By default, flatten produces a contiguous C-order copy; with order='F', it produces a Fortran-order flattening.

Choosing Between reshape, flatten, ravel, and transpose

The choice depends on what you need to do with the result:

  • Use reshape when you want to change the shape of an array while keeping the data in place. If you need a view for read-only or to avoid copying, reshape is often the right choice, but verify whether it returns a view or copy.
  • Use flatten when you need a one-dimensional copy that is independent of the original. This is useful when you want to modify the flattened result without side effects.
  • Use ravel when you want a one-dimensional array and prefer a view to save memory. If the original is contiguous, ravel avoids copying. If the original is non-contiguous, ravel may copy anyway, so flatten could be clearer if you always want a copy.
  • Use transpose when you need to permute or reverse axes, such as swapping rows and columns for a 2D array or swapping dimensions for broadcasting. Remember that the result is a view and may be non-contiguous.

A practical scenario: if you have a large 2D array and need to iterate over its columns efficiently, you might transpose it to get rows. But because the transposed view is non-contiguous, iterating row by row may be slower than if you used a contiguous copy. In that case, you could call np.ascontiguousarray(a.T) to force a copy with contiguous memory.

Common Pitfalls with reshape and transpose

One frequent issue is assuming that reshape always returns a view. If the original array is non-contiguous, reshape may copy, which can be surprising when you modify the result and expect the original to change. For example:

python
a = np.arange(12).reshape(3, 4) b = a.T # non-contiguous view c = b.reshape(2, 6) # result may be a view or a copy print(np.shares_memory(b, c)) # check whether they share memory

If you need to know whether a reshape shares memory, use np.shares_memory.

Another pitfall is using ravel on a transposed array. Since the transpose is non-contiguous, ravel must copy the data to produce a contiguous result. That is not as cheap as flattening a contiguous array, so check the memory layout before assuming ravel is the low-cost option.

Understanding the memory layout of NumPy arrays—whether they are C-contiguous, Fortran-contiguous, or neither—helps predict when these functions will copy. You can check with a.flags['C_CONTIGUOUS'] and a.flags['F_CONTIGUOUS']. This knowledge is essential for writing efficient code that avoids unnecessary copies and for preventing subtle bugs from unintended data sharing.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.