numpy arrays and shapes
Create numpy arrays, read .shape, index and slice rows, columns and ranges.
Builds on: Reading Python, Matrices and shapes
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.
An array knows its shape; brackets pick parts
The indexing rules
Worked example on a 3×4 array
Create, read shapes, slice
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
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.
Bytewise
All courses