Skip to content

Matrices as maps, and what rank counts

A data table is a matrix, and a matrix is also a machine that moves vectors. Read a matrix-vector product two ways, build the covariance matrix from the data, and count independent directions with rank.

Applied ~12 min8 steps

You will be able to

  • Read a matrix-vector product as dot products with rows and as a mix of columns
  • Build the covariance matrix from a centered data matrix and say what its entries mean
  • Explain rank, why collinear columns lower it, and why a rank-1 table is an outer product

Read first

01

A table and a machine

Read

Your data form a matrix XX with nn rows and pp columns. That is the first role of a matrix: a container.

The second role is a linear mapLinear mapWhat a matrix does to a vector: stretch, rotate, project or mix its entries, with sums and multiples carried through.. A p×pp \times p matrix WW takes a vector xx and returns a new vector WxWx. It can stretch, rotate, flip, shear or flatten, but it always keeps straight lines straight and sends the origin to itself.

Every linear model, every principal component and every layer of a neural network is a matrix used in this second way.

02

Two ways to read Wx

Read

Take W=(2103)W=\begin{pmatrix}2 & 1\\ 0 & 3\end{pmatrix} and x=(12)x=\begin{pmatrix}1\\ 2\end{pmatrix}.

  • Row by row: each output is a dot productDot productMultiply two vectors entry by entry and add: large when they point the same way, zero when they are at right angles. of a row of WW with xx: 2⋅1+1⋅2=42\cdot1+1\cdot2=4 and 0⋅1+3⋅2=60\cdot1+3\cdot2=6.
  • Column by column: WxWx mixes the columns of WW with weights from xx: 1⋅(20)+2⋅(13)=(46)1\cdot\binom{2}{0}+2\cdot\binom{1}{3}=\binom{4}{6}.

The second reading is the useful one for data: a fitted value is a weighted mix of feature columns, and a principal component score is a weighted mix of your variables.

03

The covariance matrix from the data

Read

Center each column (subtract its mean) to get XcX_c. Then

S=1n−1 Xc⊤Xc.S = \frac{1}{n-1}\,X_c^\top X_c .

Entry (j,k)(j,k) is the dot product of centered columns jj and kk, divided by n−1n-1: their covariance. The diagonal holds each column's variance. Standardize the columns first and SS becomes the correlation matrix.

This covariance matrixCovariance matrixA square table of every pair of columns' covariance, with each column's variance on the diagonal. is p×pp \times p no matter how many rows you have, and it is where PCA starts.

04

Rank: independent directions

Read

The rankMatrix rankHow many independent directions a matrix's columns span. A rank-k matrix is a sum of k simple outer products. of a matrix is how many of its columns point in genuinely new directions.

  • Add a column that is 2 × another column: the rank doesn't grow.
  • Add "total = a + b" beside aa and bb: the rank doesn't grow either.

When XX has fewer independent columns than columns, X⊤XX^\top X can't be inverted, and a regression can't tell the duplicate columns apart. That is perfect multicollinearity, and the app warns about it.

05

Rank 1 is an outer product

Read

A sales table where every store's monthly sales are its size times a month's seasonal factor is uv⊤u v^\top: one column vector times one row vector. Every row is a multiple of every other row, so its rank is 1.

A rank-kk matrix is a sum of kk such outer products. Real tables are never exactly low rank, but many are close to it. Finding the best few outer products is what the SVD does in the next lessons.

Python
import numpy as np

u = np.array([3., 1., 2.])           # store sizes
v = np.array([1., 2., 4., 2.])       # seasonal factors
X = np.outer(u, v)
print(np.linalg.matrix_rank(X))      # 1
X2 = np.column_stack([X, X[:, 0] + X[:, 1]])
print(np.linalg.matrix_rank(X2))     # still 1
R
u <- c(3, 1, 2)
v <- c(1, 2, 4, 2)
X <- outer(u, v)
qr(X)$rank                            # 1
X2 <- cbind(X, X[, 1] + X[, 2])
qr(X2)$rank                           # still 1
06

Predict

Predict

Predict first

XX has 1,000 rows and 5 columns. What shape is Xc⊤XcX_c^\top X_c?

n × np × pn × p
Reveal

p × p, here 5 × 5. A 5×10005 \times 1000 matrix times a 1000×51000 \times 5 matrix gives 5×55 \times 5: one entry per pair of columns.

07

Check: matrices

Checkpoint

Check yourself

W=(1230)W=\begin{pmatrix}1 & 2\\ 3 & 0\end{pmatrix} and x=(32)x=\begin{pmatrix}3\\ 2\end{pmatrix}. What is the first entry of WxWx?

Show the answer

Answer: 7

The first row dotted with xx: 1⋅3+2⋅2=71\cdot3+2\cdot2=7.

Check yourself

A table has columns height in cm, height in inches and weight. What is its rank (assuming height and weight aren't proportional)?

Show the answer

Answer: b

Height in inches is height in cm times a constant, so it adds no new direction. Two independent directions remain.

Check yourself

What sits on the diagonal of the covariance matrix?

Show the answer

Answer: a

Entry (j,j)(j,j) is column jj's covariance with itself, which is its variance. Ones appear only after standardizing, when the matrix is the correlation matrix.

08

Recap

Recap
  • A matrix stores data and, used as a map, mixes columns into new vectors.
  • The covariance matrix is Xc⊤Xc/(n−1)X_c^\top X_c/(n-1): one entry per pair of columns.
  • Rank counts independent directions; a rank-1 table is one outer product, and many tables are nearly low rank.

Concepts in this lesson