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.
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
A table and a machine
ReadYour data form a matrix with rows and 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 matrix takes a vector and returns a new vector . 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.
Two ways to read Wx
ReadTake and .
- 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 with : and .
- Column by column: mixes the columns of with weights from : .
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.
The covariance matrix from the data
ReadCenter each column (subtract its mean) to get . Then
Entry is the dot product of centered columns and , divided by : their covariance. The diagonal holds each column's variance. Standardize the columns first and 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 no matter how many rows you have, and it is where PCA starts.
Rank: independent directions
ReadThe 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 and : the rank doesn't grow either.
When has fewer independent columns than columns, can't be inverted, and a regression can't tell the duplicate columns apart. That is perfect multicollinearity, and the app warns about it.
Rank 1 is an outer product
ReadA sales table where every store's monthly sales are its size times a month's seasonal factor is : one column vector times one row vector. Every row is a multiple of every other row, so its rank is 1.
A rank- matrix is a sum of 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.
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 1u <- 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 1Predict
PredictPredict first
has 1,000 rows and 5 columns. What shape is ?
Reveal
p × p, here 5 × 5. A matrix times a matrix gives : one entry per pair of columns.
Check: matrices
CheckpointCheck yourself
and . What is the first entry of ?
Show the answer
Answer: 7
The first row dotted with : .
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 is column 's covariance with itself, which is its variance. Ones appear only after standardizing, when the matrix is the correlation matrix.
Recap
Recap- A matrix stores data and, used as a map, mixes columns into new vectors.
- The covariance matrix is : 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