Every Python lab in this course stores a dataset as a NumPy array first. That happens before anything else. Nothing here is deep mathematics. It's a small, fixed set of conventions. They're worth knowing well before Chapter 2 starts using them, without re-explaining each time.
Picture the same spreadsheet from Section 1, but now the row and column
numbers written on the outside start at 0 instead of 1. The very first
row is row 0. The very first column is column 0. That's the entire rule —
everything else follows from it. If a textbook says "look at x_3," and
you're translating that into code, you actually want x[2], because the
book is counting from 1 and the code is counting from 0. It feels like an
odd habit at first, but it's completely mechanical once you expect it:
subtract one from whatever position a formula names, and that is the
array index to type.
A 2D NumPy array X storing a design matrix has a .shape attribute
(n, p) — n rows (samples), p columns (features), matching Section
1's \mathbf{X} exactly. Indexing is 0-based: X[0] is the first
row, not a special or invalid index. X[i, j] reads row i, column j
— row 3's second feature is X[2, 1], not X[3, 2], because both the row
count and the column count start at 0. A single row, X[i, :], is that
sample's feature vector; a single column, X[:, j], is every sample's
value for one feature — the colon means "every index along this axis."
Negative indices count from the end: X[-1] is the last row. This is the
one place NumPy's convention and ordinary mathematical vector/matrix
notation quietly disagree — a textbook's x_1 means the first entry,
which is x[0] in code, not x[1].
Reading rows and columns off a real array
Step through the multiplication. Row 2 of A (the third row, since indexing starts at 0) is [12, 2, 30] -- confirm that A[2, 0] is 12, matching the running answer's first term in that row.
Loading interactive…
Worked example
Converting a formula's index to a NumPy index
-
1
A formula refers to x_1, the first entry of a feature vector
x. What NumPy expression retrieves that same value? -
2
Mathematical notation here counts from 1; NumPy counts from 0. The first entry is at position 0 in code.
-
3\boxed{\texttt{x[0]}}
This offset-by-one translation is worth checking every single time a formula and a line of code sit side by side in this course's Python labs.
Loading interactive…
Worked example
Selecting a row vs. a column
-
1
For the same 3\times 3 array
A, what is the difference betweenA[1, :]andA[:, 1]? -
2
The colon's position marks which axis is "every index" — first position is rows, second is columns.
-
3\boxed{\texttt{A[1, :]} \text{ is row 1 (a sample); } \texttt{A[:, 1]} \text{ is column 1 (a feature)}}
Loading interactive…
Where this goes next
Every EDA and preprocessing chapter (3-4) slices a NumPy array by row (selecting samples) or by column (selecting features) using exactly this notation before any model ever sees the data.
Check your understanding (counts toward your progress)
In a NumPy array X, what does X[0] refer to?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
NumPy indexing starts at 0, so index 0 is the first row, not the second.
The last row is X[-1], not X[0], unless the array has only one row.
NumPy indexing is 0-based, so index 0 is always valid and refers to the first element along that axis.
Why
NumPy arrays are 0-indexed, so X[0] is the first row -- the single most important convention this section establishes.
What is the practical purpose of a NumPy array's .shape attribute for a design matrix?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Data type is reported by .dtype, a different attribute entirely.
.shape only reports dimensions -- it never reorders or sorts the array's contents.
Computing a mean is a separate operation (.mean()), unrelated to reading an array's shape.
Why
.shape returns a tuple (n, p) for a 2D array -- exactly the sample count and feature count Section 1 defined for a design matrix.
For a 2D array X with shape (n, p), what does X[:, 1] select?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
That describes X[1, :], selecting a row -- the colon in X[:, 1] is on the row axis, not the column axis.
The colon means "every index along this axis," not "no change" -- X[:, 1] genuinely selects one column across all rows.
The colon selects every row for that column, not just the first one.
Why
A colon on the row axis with a fixed column index selects that whole column -- every sample's value for feature index 1.