All courses
Courses › How LLMs really work · foundation

numpy arrays and shapes

Create numpy arrays, read .shape, index and slice rows, columns and ranges.

Builds on: Reading Python, Matrices and shapes

▶ Watch this lesson Start the whole course

What the animation says

A table that knows its shape

Every model in this course is built from tables of numbers. Take a few tokens, each one a row of numbers. Stack the rows, and you get a matrix. In code, that table is a numpy array. Here is a small one: three rows, four columns. The array knows its own size. Its shape is a tuple with one entry per axis: rows first, then columns. And counting starts at zero, so the rows are numbered zero, one, two.

One position per axis

To pick a piece, use square brackets, with one position per axis, separated by commas. A single number picks one spot. This line asks for row one, which is the second row: five, six, seven, eight. Its shape is just four. The row axis is gone, because we picked exactly one row. A bare colon means everything along that axis. So this line keeps every row, and picks column two. Your turn: tap the column it picks.

Slices stop before the end

Now ranges. A slice like zero colon two starts at the first number and stops just before the second. So it means rows zero and one. Two minus zero: two rows. Slice both axes and you cut a block: rows zero and one, columns one and two. Shape two by two. Leave out the start, it begins at zero. Leave out the end, it runs to the end. So what shape do you get from rows two up to three?

Index drops, slice keeps

It's one by four. Rows two up to three is just one row, but a slice keeps the row axis. Compare picking row two with a single index: shape four. The axis is gone. That's the rule. A single index drops its axis. A slice keeps it, with length end minus start. Mix them, axis by axis. Rows zero to two: a slice, keep it, length two. Column three: an index, drop it. Shape: two. Now a bigger array, six rows by eight columns. What shape is this block?

Shapes in real code

Three by two. Five minus two rows, two minus zero columns. Here's the whole idea in code: make a six by eight array of zeros, set one entry, and print shapes. Row two has shape eight. Column three has shape six. Rows one to four: three by eight. Negative indices count from the end: minus one is the last row or column. And when the maths says row t, the code says t minus one, because counting starts at zero. Shape is rows, then columns. An index drops, a slice keeps.

Picking pieces out of a table of numbers

Underneath, every model in this course is built from big tables of numbers. A sequence of $T$ tokens, each a row of $d$ numbers, is a $T\times d$ matrix. In code, these tables are **numpy arrays**. You will constantly meet lines like `X[:, 3]` or `X[2:5].shape`. Can you instantly say how big the table is and which piece a line picks out?

An array knows its shape; brackets pick parts

A numpy array is a table of numbers, all of the same type. It knows its own size: `.shape` is a tuple with one entry per axis, which is `(rows, columns)` for a table. You pick parts with square brackets: one position per axis, separated by commas. Each position is either a single index (pick one) or a slice `a:b` (a range that stops just *before* `b`). A bare `:` means "everything along this axis".

The indexing rules

For `X` with `X.shape == (T, d)`: - `X[i, j]`: one number - `X[i]` (same as `X[i, :]`): row $i$, shape `(d,)` - `X[:, j]`: column $j$, shape `(T,)` - `X[a:b]`: rows $a$ to $b-1$, shape `(b-a, d)` (for $0\le a\le b\le T$) - `X[a:b, c:e]`: a block, shape `(b-a, e-c)` - Leave out the start and the slice begins at 0 (`:3` means `0:3`). Leave out the end and it runs to the end (`1:` means index 1 onward). - Negative indices count from the end: `X[-1]` is the last row, and `X[-2:]` is the last two rows. A single index **drops** that axis; a slice **keeps** it. Counting starts at 0, so maths row $t$ is `X[t-1]`.

Worked example on a 3×4 array

`X = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])` - `X.shape` → `(3, 4)`: 3 rows, 4 columns. - `X[1]` → the second row `[5,6,7,8]`, shape `(4,)`. - `X[:, 2]` → the 3rd entry of every row, `[3,7,11]`, shape `(3,)`. - `X[0:2, 1:3]` → rows 0–1 and columns 1–2: `[[2,3],[6,7]]`, shape `(2,2)`. - `X[-1, 0]` → last row, first column: `9`.

Create, read shapes, slice

Create a $6\times 8$ array of zeros, set one entry, then check the shape of each piece you cut out. The last line shows that a slice keeps its axis.
import numpy as np
X = np.zeros((6, 8))      # 6 rows, 8 columns of 0.0
X[2, 5] = 7.0             # set one entry
print(X.shape)            # (6, 8)
print(X[2].shape)         # (8,)   row 2
print(X[:, 3].shape)      # (6,)   column 3
print(X[1:4].shape)       # (3, 8) rows 1, 2, 3
print(X[1:4, 0:2].shape)  # (3, 2) a block
print(X[2, 5], X[-1, -1]) # 7.0 0.0
print(X[2:3].shape)       # (1, 8) slice keeps the axis

Remember

`X.shape` is `(rows, columns)`. `X[i]` gives a row, `X[:, j]` gives a column, and `a:b` means $a$ up to $b-1$, so it covers $b-a$ items. A missing start means 0 and a missing end means "to the end". A single index drops an axis, a slice keeps it, and counting starts at 0.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.