Review

Core idea

Predict the result shape before running an array operation.

Elementwise operations

Elementwise operations apply by position:

Elementwise multiplication is not a dot product.

Reductions

Reductions summarize values:

Without keepdims, the reduced axis disappears.

Broadcasting

Broadcasting combines compatible shapes.

Example:

(4, 3) + (3,) -> (4, 3)

Read shapes from the right.

Compared dimensions must be equal, or one of them must be 1. A missing leading dimension also behaves like 1.

Vectorization

Vectorization expresses numerical work as array operations:

Use tiny examples to check vectorized code.

Matrix-vector products

(rows, features) @ (features,) -> (rows,)

The vector length must match the number of columns.

Matrix-matrix products

(m, n) @ (n, p) -> (m, p)

The inner dimensions must match. The outer dimensions remain.

Shape debugging

When an operation fails:

  1. print each shape;
  2. write the intended shape equation;
  3. name each axis;
  4. reduce to a tiny example;
  5. fix the violated assumption.