Linear algebra in plain words: a tour of the core objects
Before this lesson: nothing. You need vectors as lists of numbers, such as , and the idea of adding two vectors and multiplying a vector by a number. This lesson is an optional tour: every object gets one picture and one formula, and a link to the lesson that treats it in full. Time: about 50 minutes, plus the notebook.
Why this matters #
Almost every model in machine learning is built from matrices. A linear layer computes . Attention compares tokens with dot products, . LoRA fine-tunes a model with a low-rank update . PCA compresses data with the SVD. A covariance matrix is positive definite, and the Hessian of a loss is positive definite near a strict minimum. This lesson gives you the picture behind each of these words, so that the formulas of the next lessons have something to attach to.
1. Predict first #
2. The picture #
A matrix moves space #
Think of a matrix as an action on space, not only as a table of numbers. A matrix takes every point of the plane to a new point . Write and . Then the first column of is and the second column is : the columns are where the basic arrows land. Every other point follows. With the columns of ,
So the grid of the plane goes to a grid of parallelograms, and the origin stays where it is.
Drag the orange and blue tips. They are the columns of , the images of and . The green unit square goes to the parallelogram they span. Try the presets: a stretch, a rotation, a shear, a reflection, and a matrix that flattens the plane onto a line.
In ML: a linear layer is this move; its output is a combination of the columns of . Full lesson: lesson 01.
Span, basis and dimension #
The span of some vectors is the set of all points you can reach by combining them: for all numbers . One arrow spans a line, two non-parallel arrows span a plane. Vectors are linearly independent when none of them is a combination of the others, so each one adds a new direction. A basis is a set of independent vectors that spans the whole space: a choice of axes. The dimension is the number of vectors in a basis.
Drag to rotate the figure. Here lies in the plane of and , so it adds no new direction: the span (green) is only a plane, and the three vectors are dependent. Edit one entry of to lift it off the plane: the span becomes all of , and the three vectors form a basis.
In ML: the columns of a weight matrix span the set of all outputs of the layer. Full lesson: lesson 01.
Rank: the directions that survive #
The rank of a matrix is the number of independent directions that are still there after the move. It is the dimension of the set of outputs. If a matrix flattens space onto a plane, its rank is 2: one direction was squashed to 0. The inputs that go to 0 form the null space. Rank and null space share the input dimensions: (lesson 02 proves it).
Press Squash (play), or move the slider from 0 to 1. The grey cube is the input, the aqua shape is its image. With rank 2 the cube becomes a flat parallelogram inside a plane, and the orange null-space line shrinks to the point 0. Pick rank 3 (a solid box, nothing lost) and rank 1 (everything lands on one line, and a whole plane goes to 0).
In ML: low rank means redundancy. LoRA writes a weight update as with a small inner size , so its rank is at most . Full lesson: lesson 02.
A matrix flattens all of onto a plane through 0. What are its rank and its determinant?
The outputs fill a plane, so 2 independent directions survive: rank 2. A flat image has volume 0, so the determinant (the volume factor) is 0. Rank 3 would mean that no direction is lost; rank 1 would mean that everything lands on a line.
3. Definitions and main results #
This section is a tour. For each object: one plain sentence, a picture or a small example, one formula, one use in ML, and the lesson that treats it in full. There are no proofs here.
Dot product, length and orthogonality #
The dot product measures how much two vectors point the same way. For ,
where is the norm (the length) and is the angle between the vectors. The sign tells the angle: positive means below , negative means above , and zero means the vectors are orthogonal (perpendicular).
Drag the orange tip and the blue tip . The thick segment is the shadow of on the line through . It is aqua when and magenta when , and it disappears when . Check that and always agree. Try perpendicular: .
In ML: cosine similarity of embeddings is ; attention scores are dot products . Full lesson: lesson 03.
and . What does tell you about the two vectors?
, and both vectors are nonzero, so and . Opposite directions would give , that is .
Eigenvalues and eigenvectors #
Most directions are turned by a matrix. An eigenvector of a square matrix is a nonzero vector whose direction is not turned: lies on the line through . The factor is the eigenvalue :
The factor can be larger than 1 (stretch), between 0 and 1 (shrink), negative (the arrow flips to the other side of the line), or 0 (the direction is flattened). A rotation of the plane by turns every direction, so it has no real eigenvector; its eigenvalues are the complex numbers .
Drag around the circle and watch . Usually points off the line through . For there are two directions where it does not: is stretched by , and is flipped, , so . Press Show eigenvectors to check. Then pick rotation 90°: no direction stays on its line.
In ML: the eigenvectors of a covariance matrix are the principal directions of PCA; the eigenvalues of a Hessian give the curvature of a loss. Full lesson: lesson 05 (and lesson 06 for symmetric matrices).
sends to . What does this say?
, so with . An eigenvector may be flipped: it only has to stay on its own line. The eigenvalue keeps the sign, so it is , not . The null space would need .
Determinant #
The determinant of a square matrix is the factor by which it multiplies areas (in 2D) or volumes (in 3D), with a sign for orientation. For a matrix,
Small examples: , so every area is multiplied by 6. The reflection has determinant : areas are kept, but the plane is mirrored. The matrix has parallel columns and : the plane is flattened onto a line. A determinant of 0 means the move loses a direction and cannot be undone. The determinant is also the product of the eigenvalues.
In ML: the density of a normalizing flow needs ; the Gaussian likelihood needs . Full lesson: lesson 03.
The SVD: rotate or reflect, stretch, rotate or reflect #
The singular value decomposition (SVD) says that every matrix, square or not, does three simple moves in a row:
First turns space (a rotation, possibly with a reflection). Then stretches along the axes by the singular values . Then turns again (again a rotation, possibly with a reflection). So the unit circle always becomes an ellipse, and the half-axes of the ellipse are the singular values.
Press ▶ Play, or step through the three factors. The shear turns the circle, stretches it to half-axes and , and turns it again. Its eigenvalues are and : singular values and eigenvalues are different things.
Singular values are not eigenvalues. Eigenvalues ask "which directions keep their line?"; singular values ask "how long are the axes of the ellipse?". They agree only for special matrices, such as symmetric matrices with no negative eigenvalues. Keeping the largest singular values gives the best low-rank approximation of a matrix (Eckart–Young, lesson 08).
In ML: compression of weight matrices, LoRA initializations, the pseudoinverse in least squares. Full lesson: lesson 08.
The shear has eigenvalues and . What are its singular values?
The singular values are the square roots of the eigenvalues of , which are and . So , . Their product is , but neither of them is 1: the shear turns the circle into a tilted ellipse.
PCA: the directions where the data spreads most #
You have data points in , the rows of a matrix . Principal component analysis (PCA) finds the direction in which the cloud of points is most spread out (the first principal component), then the most spread-out direction perpendicular to it, and so on. Describing each point by its first few components compresses the data. The recipe:
- Center the data: subtract the mean of each column, so the cloud sits around 0.
- Take the SVD of the centered matrix: .
- The columns of are the principal directions. The variance along direction is .
Drag the orange direction . Each point casts a shadow on the line through . The chart shows the variance of the shadows for every angle of : it is largest at PC1, the long axis of the cloud (about here), and smallest at PC2, perpendicular to it. Press Snap w to PC1 to jump there.
In ML: visualizing embeddings, whitening inputs, compressing features; in science, empirical orthogonal functions (EOFs) in climate data. Full lesson: lesson 09.
Positive definite matrices #
A symmetric matrix is positive definite (PD) if for every . Equivalently, all its eigenvalues are positive. Picture the graph of : for a PD matrix it is a bowl with a single lowest point at 0. Small examples:
- : for . PD, a bowl.
- : , which is at . Not PD: a saddle.
In ML: every covariance matrix is positive semidefinite ( instead of ); a loss is locally a bowl when its Hessian is PD. Full lesson: lesson 07.
Factorization: the idea behind all of them #
Many results above say the same thing: write a matrix as a product of simple matrices to see what it does. "Factorization" and "decomposition" mean the same.
| Factorization | Works for | Simple pieces | Main use | Lesson |
|---|---|---|---|---|
| square ( swaps rows) | lower and upper triangular | solve | 10 | |
| any, columns independent | orthonormal columns, upper triangular | least squares | 12 | |
| (Cholesky) | symmetric PD | lower triangular | Gaussians, fast solves | 07 |
| square with independent eigenvectors | eigenvectors, eigenvalues | powers , dynamics | 05 | |
| symmetric | orthonormal eigenvectors, real eigenvalues | quadratic forms, PCA | 06 | |
| every matrix | orthogonal, diagonal , orthogonal | low rank, PCA, pseudoinverse | 08 |
Answers to section 1. (1) Yes. : the direction is stretched by 3. There is a second one, , which keeps unchanged: , eigenvalue 1. (2) No. Flattening loses a direction: many points land on the same point of the plane, and no matrix can send one point back to many. The rank is 2 and the determinant is 0.
4. Worked example #
One matrix for everything: .
(a) Columns. and . The unit square goes to the parallelogram with corners , , , .
(b) Determinant and rank. . Every area is multiplied by 3, and the orientation is kept (the sign is ). The columns are not parallel, so the rank is 2 and can be undone (it is invertible).
(c) Eigenvectors, by testing directions.
- : not on the line through . Not an eigenvector.
- . Eigenvector, .
- . Eigenvector, .
Two checks: the product , and the sum , the sum of the diagonal entries (the trace). The two eigenvectors are perpendicular: . This always happens for symmetric matrices (lesson 06).
(d) Positive definite. Both eigenvalues are positive, so is PD. Directly: for .
(e) The SVD equals the eigendecomposition here. Put the unit eigenvectors in and the eigenvalues in . Then
has orthonormal columns and the eigenvalues are , so this is also an SVD with and : the singular values are 3 and 1. Note that : this is a reflection, not a rotation. That is why the SVD slogan says "rotate or reflect".
(f) PCA. If is the covariance matrix of a data cloud, the cloud is most spread out along (variance 3) and least along (variance 1). These are the two principal directions.
For , compute .
. The vector lies on the line through , the eigenvector with eigenvalue 1, so leaves it unchanged. would be the answer for eigenvalue 3, which belongs to the direction .
5. In code #
NumPy checks the worked example. The notebook for this lesson has these cells and the exercises.
import numpy as np
S = np.array([[2., 1.], [1., 2.]])
print(S @ [1., 0.], S @ [0., 1.]) # [2. 1.] [1. 2.]: the columns
print(np.linalg.matrix_rank(S)) # 2
print(round(np.linalg.det(S), 12)) # 3.0
lam, Q = np.linalg.eigh(S) # symmetric S; eigenvalues in increasing order
print(lam) # [1. 3.]
print(np.allclose(S @ Q, Q * lam)) # True: S q_i = lam_i q_i
print(np.linalg.svd(S, compute_uv=False)) # [3. 1.]: same as the eigenvalues
A = np.array([[1., 1.], [0., 1.]]) # the shear
print(np.linalg.eigvals(A).real) # [1. 1.]
print(np.linalg.svd(A, compute_uv=False)) # [1.61803399 0.61803399]
# PCA = SVD of the centered data
rng = np.random.default_rng(0)
X = rng.multivariate_normal([5., -2.], S, size=500) # covariance S
Xc = X - X.mean(axis=0) # step 1: center
U, s, Vt = np.linalg.svd(Xc, full_matrices=False) # step 2: SVD
print(np.round(Vt, 2)) # rows ±(0.71, 0.7) and ±(-0.7, 0.71)
print(np.round(s**2 / (len(X) - 1), 2)) # [2.91 0.94]: close to 3 and 1
The signs of eigenvectors and singular vectors are arbitrary: is as good as . The sample variances 2.91 and 0.94 are close to, but not equal to, 3 and 1, because 500 points are a finite sample.
6. Where it shows up #
- Neural networks. Every linear layer is a matrix acting on a vector; a network is a chain of such moves with nonlinear functions in between.
- Attention. The score of a query against a key is the dot product ; the score matrix has rank at most the head dimension.
- Fine-tuning. LoRA learns a low-rank update with a small rank .
- Optimization. Near a strict minimum the Hessian is positive definite (a bowl); its eigenvalues set how fast gradient descent converges in each direction.
- Data analysis. PCA (via the SVD) for visualization and compression; in climate science the same method is called EOF analysis, and in fluid dynamics proper orthogonal decomposition (POD).
- Probabilistic models. Covariance matrices are positive semidefinite; Gaussian likelihoods and normalizing flows need log-determinants.
7. Common mistakes #
- "An eigenvector keeps its direction exactly, so ." It only has to stay on its line. can be negative (the arrow flips), zero, or complex (a rotation has no real eigenvector).
- "Singular values are the eigenvalues." Only for symmetric matrices with no negative eigenvalues. The shear has eigenvalues and singular values .
- "PCA on raw data." Without centering, the first direction points towards the mean of the data, not along the spread. Subtract the column means first.
- "The SVD is rotate, stretch, rotate." and are orthogonal matrices, which can be reflections (), as in the worked example.
- "Independent means pairwise not parallel." , and are pairwise not parallel, but they are dependent: .
8. Check yourself #
- Which matrix rotates the plane by counter-clockwise? Use the images of and .
Answer
and , so . It has no real eigenvector: every direction is turned. - What is the rank of , and what does it do to the plane?
Answer
Rank 1: the second column is twice the first. The plane is flattened onto the line through ; the direction goes to 0. The determinant is . - , . Compute and . Is the angle acute?
Answer
, , so . Positive, so the angle is acute (about ). - A matrix has eigenvalues and . What is its determinant? Does it keep the orientation of the plane?
Answer
. Areas are doubled, and the negative sign means the orientation is reversed (a mirror image). - Why must you center the data before you take the SVD in PCA?
Answer
PCA measures spread around the mean. The SVD of the raw data measures spread around 0, so if the cloud is far from 0, the first singular vector points towards the cloud's mean, not along its long axis.
What is column of a matrix , seen as a map?
, the image of the -th standard basis vector. The columns fix the whole map.
What does the rank of a matrix count?
The number of independent directions that survive the map: the dimension of the set of outputs .
A matrix flattens space onto a plane. Give its rank and its determinant.
Rank 2 and . One direction (the null space) is sent to 0.
What does the sign of tell you?
Positive: the angle is below . Zero: the vectors are perpendicular. Negative: the angle is above .
Define eigenvector and eigenvalue.
A nonzero with : keeps on its own line. The factor is the eigenvalue; it can be negative (a flip).
Why does a rotation of the plane have no real eigenvector?
It turns every direction, so no nonzero stays on its own line. Its eigenvalues are .
What does measure?
The factor by which multiplies areas or volumes, with a sign for orientation. means flattens space.
What three moves make up the SVD ?
rotates or reflects, stretches along the axes by the singular values , and rotates or reflects.
How do you compute PCA with the SVD?
Center the data (subtract each column mean), take . The columns of are the principal directions; the variances are .
When is a symmetric matrix positive definite?
When for every ; equivalently, when all its eigenvalues are positive. Its quadratic form is a bowl.
9. Interview angle #
- "Explain eigenvectors in one sentence." — The directions that a matrix keeps on their own line; the eigenvalue is the stretch factor along that line, and it can be negative.
- "What is the difference between the eigendecomposition and the SVD?" — The eigendecomposition needs a square matrix with enough eigenvectors, and its eigenvectors need not be orthogonal. The SVD exists for every matrix and uses two orthonormal bases.
- "How is PCA related to the SVD?" — PCA is the SVD of the centered data matrix: right singular vectors are the principal directions, are the variances.
- "What does a determinant of 0 mean?" — The matrix flattens space: rank below , a nonzero null space, no inverse.
10. Practice #
The notebook for this lesson runs in Colab. It has three short exercises, each with a check cell and a folded solution:
- Name the map: look at what four matrices do to a grid and name each one (stretch, rotation, shear, reflection, flatten).
- Find the eigenpairs of a symmetric matrix with
np.linalg.eigh, and check . - PCA of a 2D cloud with the SVD of the centered data, compared with
sklearn.decomposition.PCA.
11. Watch and read #
- 3Blue1Brown, Essence of linear algebra: the whole series, with text versions of each chapter. Watch it once for the pictures.
- Gilbert Strang, A 2020 Vision of Linear Algebra (MIT OpenCourseWare): six short lectures on the same objects, from to the SVD.
- Boyd and Vandenberghe, Introduction to Applied Linear Algebra (VMLS), free PDF: chapters 1–3 (vectors, linear functions, norms and angles) for the beginner background.
Runs in your browser with nothing to install. The notebook repeats the key demos and has exercises with checks; solutions are inside, folded.
No Google account needed: Python runs inside your browser. The first start downloads about 40 MB; your changes are saved in this browser only.