Reshaping and stacking

Intermediate

Introduction

Data rarely arrives in the shape you need. A sensor sends 24 readings in one row that you want as 4 rows of 6; a model needs a column vector, not a flat list; two months of data must be joined into one table. NumPy changes shapes instantly, usually without copying any data, because the shape is only a description of how to read the same block of memory.

How it works

reshape. a.reshape(r, c) reads the elements in row-major order (along each row, then the next row) and lays them out in the new shape. The total must stay the same:

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

A -1 asks NumPy to compute one dimension from the total: 6 elements in 3 rows means 2 columns. Only one -1 is allowed, and if the sizes do not fit, NumPy raises a ValueError ("cannot reshape array of size 6 into shape ..."). a.reshape(-1, 1) turns a 1-D array into a column, a common need in machine learning.

Flattening. a.ravel() and a.flatten() both give a 1-D array. ravel returns a view when it can, so it is fast but shares memory; flatten always copies.

Transposing. a.T swaps the axes: shape (2, 3) becomes (3, 2), and row i becomes column i. It is a view, not a copy.

Joining. np.concatenate([a, b], axis=0) puts b under a; every dimension except axis 0 must match. axis=1 puts b to the right of a, so the number of rows must match. np.vstack and np.hstack are shortcuts for these two cases. np.stack([a, b]) is different: it adds a new axis, so two arrays of shape (3,) become one of shape (2, 3).

x = np.array([[1, 2], [3, 4]])
print(np.concatenate([x, [[5, 6]]], axis=0).shape)   # (3, 2)
print(np.hstack([x, x]).shape)                       # (2, 4)

Example

# Reshaping modelled with lists: the same six values in different shapes.
flat = [1, 2, 3, 4, 5, 6]


def reshape(values, rows, cols):
    assert rows * cols == len(values)
    return [values[r * cols:(r + 1) * cols] for r in range(rows)]


a = reshape(flat, 2, 3)
print("reshape(2, 3):", a)
print("reshape(3, -1):", reshape(flat, 3, len(flat) // 3))
print("a.T:", [list(col) for col in zip(*a)])
print("a.ravel():", [x for row in a for x in row])
print("vstack with [7, 8, 9]:", a + [[7, 8, 9]])
print("hstack with a column of 0s:", [row + [0] for row in a])
Open in compiler

Line by line

  1. reshape slices the flat list into rows of cols items, the row-major order NumPy uses.
  2. len(flat) // 3 plays the part of -1: the missing dimension is the total divided by the known one.
  3. zip(*a) turns columns into rows, modelling a.T.
  4. Adding a row models vstack; adding an item to each row models hstack with a column.

Expected result

reshape(2, 3): [[1, 2, 3], [4, 5, 6]]
reshape(3, -1): [[1, 2], [3, 4], [5, 6]]
a.T: [[1, 4], [2, 5], [3, 6]]
a.ravel(): [1, 2, 3, 4, 5, 6]
vstack with [7, 8, 9]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
hstack with a column of 0s: [[1, 2, 3, 0], [4, 5, 6, 0]]

In the real world

Images are reshaped from flat pixel lists into (height, width, 3) arrays; batches of images are stacked into 4-D arrays for neural networks. Time series are reshaped into (days, hours) to compare the same hour across days. Monthly files are concatenated into one yearly table.

Common mistakes

  • Expecting reshape to transpose: reshape(3, 2) of a (2, 3) array is not its transpose.
  • Writing to a raveled array or a transpose and being surprised that the original changes; they are views.
  • Concatenating arrays whose other dimensions differ, which raises an error.
  • Using stack when concatenate was meant, which adds an unexpected axis.

Best practices

  • Print .shape after every reshape while you develop.
  • Use -1 to avoid hard-coding sizes that depend on the data.
  • Call .copy() when you need a reshaped or transposed array that is independent.

Summary

reshape lays the same elements out in a new shape in row-major order, with -1 standing for one computed dimension. ravel and flatten give 1-D arrays, .T swaps rows and columns, concatenate, vstack and hstack join along an existing axis, and stack adds a new one.

Quick check

1. What is the shape of np.arange(12).reshape(3, -1)?
2. a has shape (2, 5). What is the shape of a.T?
3. Two arrays of shape (3,) passed to np.stack give shape…
4. Which one always returns a copy?

Key takeaways

  • reshape keeps the elements in row-major order; the sizes must multiply to the same total.
  • Use -1 for at most one dimension.
  • concatenate joins along an existing axis, stack creates a new one.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.