Section B.8 Constructing Common Arrays and Design Matrices
This section helps you read code that creates standard arrays, identity matrices, sample grids, and design matrices.
np.ones(4)
np.ones_like(t)
np.full_like(t, 2.0)
np.zeros((2, 3))
np.eye(3)
t = np.array([0.0, 1.0, 2.0])
X = np.column_stack([np.ones_like(t), t])
s = np.array([5.0, 2.0, 1.0])
Sigma = np.diag(s)
xs = np.linspace(-1, 1, 41)
xx, yy = np.meshgrid(xs, xs)
points = np.vstack([xx.ravel(), yy.ravel()]).T
mask = np.triu(np.ones_like(S), k=1).astype(bool)
Ones vector.
np.ones(4)
Read as. A vector of ones.
Shape/return. Shape
(4,).
Used for. Simple test data.
Matching ones.
np.ones_like(t)
Read as. Ones with the same shape as
t.
Shape/return. Same shape as
t.
Used for. Constant feature column.
Matching constant array.
np.full_like(t, value)
Read as. Fill an array with the same shape as
t using one value.
Shape/return. Same shape as
t.
Used for. Baseline arrays and plotting helpers.
Zero array.
np.zeros((m, n))
Read as. An \(m\times n\) zero array.
Shape/return. Shape
(m, n).
Used for. Placeholders and test data.
Identity matrix.
np.eye(n)
Read as. The identity matrix.
Shape/return. Shape
(n, n).
Used for. Inverses and rank checks.
Column stack.
np.column_stack([...])
Read as. Put several one-dimensional arrays side by side as columns.
Shape/return. A two-dimensional array.
Used for. Design matrices in least-squares models.
Affine polynomial design matrix.
A = np.column_stack([np.ones_like(xs), xs])
Read as. Put the sampled constant feature and sampled \(x\)-feature into columns.
Shape/return. A two-column design matrix.
Used for. Sampled least-squares line fitting.
Quadratic polynomial design matrix.
A2 = np.column_stack([np.ones_like(xs), xs, xs**2])
Shape/return. A three-column design matrix.
Used for. Sampled least-squares quadratic fitting.
Sampled plane design matrix.
A = np.column_stack([np.ones_like(xx), xx, yy])
Shape/return. A three-column design matrix.
Used for. Sampled best-fit planes.
Diagonal matrix.
np.diag(s)
Read as. Build a diagonal matrix with entries of
s on the diagonal.
Shape/return. A square matrix when
s is one-dimensional.
Used for. SVD reconstruction.
Sample grid.
np.linspace(a, b, N)
Read as. Evenly spaced sample points.
Shape/return. A one-dimensional array.
Used for. Plotting and sample grids.
Two-variable sample grid.
np.meshgrid(grid, grid)
Read as. Build coordinate arrays for all pairs of grid values.
Used for. Sampling a two-variable function on a grid.
Vertical stack.
np.vstack([a, b])
Read as. Stack arrays as rows.
Shape/return. A two-dimensional array.
Used for. Turning sampled coordinates into a point table.
Flatten a grid.
X.ravel()
Read as. List all entries of
X in a one-dimensional array.
Shape/return. A one-dimensional array.
Used for. Turning grid coordinates into sample-point columns.
Attention mask.
np.triu(np.ones_like(S), k=1).astype(bool)
Read as. The optional attention mask pattern.
Shape/return. A Boolean array.
Used for. Optional attention code.
Design matrices in least squares often use
np.column_stack to build one column per feature. The attention-mask pattern is optional and is explained in Appendix B.13.
