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:
- print each shape;
- write the intended shape equation;
- name each axis;
- reduce to a tiny example;
- fix the violated assumption.