ML Foundations Lecture 8 of 14
Part III — Linear Algebra · Lecture 8

Vectors, Span, and Geometry

A data point is a vector. A model's weights are a vector. A gradient is a vector. This lecture builds the object and its geometry — length, angle, projection — because every later result in the course is a geometric statement wearing algebraic clothes.

By the end you can

  • State the vector space axioms and check them for \(\RR^d\)
  • Compute a span and decide whether a set of vectors is linearly independent
  • Derive \(\vu\cdot\vv = \|\vu\|\|\vv\|\cos\theta\) from the law of cosines
  • Derive the orthogonal projection formula and interpret it as 'best approximation'
  • Explain cosine similarity, and why embeddings are normalised

Assumed

  • Lectures 1–7 (the calculus half). No prior linear algebra assumed.
  • Pythagoras' theorem

1Data lives in ℝᵈ

One house: 120 square metres, 3 bedrooms, 15 years old, 2 km from the station. Write it as an ordered list:

\[\vx = \begin{bmatrix} 120 \\ 3 \\ 15 \\ 2\end{bmatrix} \in \RR^4.\]

That is a vector: a point in a four-dimensional space, one axis per feature. A dataset of \(n\) houses is \(n\) such points. Stack them as rows and you have the design matrix \(\mX\in\RR^{n\times d}\), the object every supervised learning algorithm takes as input.

We cannot picture \(\RR^4\), let alone \(\RR^{784}\). But nearly everything we need is two-dimensional geometry that happens to be written in a way that does not care about the dimension. Learn it in the plane, where you can see it; the algebra then carries it anywhere.

Practical engineering note

Two conventions to fix now, because mismatching them causes more bugs than any other single thing in ML code. Vectors are columns by default: \(\vx\in\RR^d\) means a \(d\times1\) object. Data matrices are rows-as-samples: \(\mX\in\RR^{n\times d}\) has one row per data point. So the \(i\)-th sample is the column vector \(\vx_i = \mX[i,:]^\top\). This is why you constantly see \(\mX\vw\) (an \(n\)-vector of predictions) rather than \(\vw^\top\mX\).

2What a vector space is

A vector is not "a thing with magnitude and direction" — that is a description of one example. The real definition is behavioural: a vector is an element of a set on which you can add and scale, subject to rules.

Definition — vector space

A vector space over \(\RR\) is a set \(V\) with two operations, addition \(\vu+\vv\) and scalar multiplication \(\alpha\vu\), satisfying, for all \(\vu,\vv,\vw\in V\) and \(\alpha,\beta\in\RR\):

  1. \(\vu+\vv = \vv+\vu\)  (commutativity)
  2. \((\vu+\vv)+\vw = \vu+(\vv+\vw)\)  (associativity)
  3. There is \(\vzero\in V\) with \(\vu+\vzero=\vu\)  (zero vector)
  4. For each \(\vu\) there is \(-\vu\) with \(\vu+(-\vu)=\vzero\)  (inverses)
  5. \(\alpha(\vu+\vv)=\alpha\vu+\alpha\vv\) and \((\alpha+\beta)\vu = \alpha\vu+\beta\vu\)  (distributivity)
  6. \(\alpha(\beta\vu)=(\alpha\beta)\vu\) and \(1\vu=\vu\)

\(\RR^d\) with componentwise operations satisfies all of them — each axiom reduces to the corresponding property of real numbers, applied one coordinate at a time. But so do many other things, and that is the payoff of abstraction:

Every theorem we prove using only the axioms applies to all of them at once. That is why "function space" methods in machine learning are not an analogy: they are the same theorems.

3Linear combinations and span

Definition — linear combination, span

A linear combination of \(\vv_1,\dots,\vv_k\) is any vector of the form

\[ \alpha_1\vv_1 + \alpha_2\vv_2 + \cdots + \alpha_k\vv_k, \qquad \alpha_i\in\RR. \]

Their span is the set of all such combinations:

\[ \operatorname{span}\{\vv_1,\dots,\vv_k\} = \left\{\textstyle\sum_i \alpha_i\vv_i \;:\; \alpha_i\in\RR\right\}. \]

It is always itself a vector space — a subspace of the ambient one.

Span of two vectors — drag the coefficients, and watch what happens when the vectors alignInteractive
v₁v₂α₁v₁ + α₂v₂reachable set (the span)
1.20
0.80
70
1.60
The shaded region is everything reachable by some choice of α₁, α₂. With v₁ and v₂ pointing in different directions the span is the whole plane. Slide the angle to 0 or 180 degrees: the two vectors become parallel, one is now redundant, and the span collapses to a single LINE — you have lost a dimension. That collapse is linear DEPENDENCE, and in machine learning it is what makes a design matrix rank-deficient and a least-squares problem ill-posed.
Definition — independence, basis, dimension

\(\vv_1,\dots,\vv_k\) are linearly independent if the only way to write the zero vector as a combination of them is the trivial way:

\[ \alpha_1\vv_1+\cdots+\alpha_k\vv_k = \vzero \quad\Longrightarrow\quad \alpha_1=\cdots=\alpha_k=0. \]

They are dependent otherwise — meaning at least one of them is a combination of the rest, and therefore redundant.

A basis for a space \(V\) is a set that is both independent and spans \(V\). Every basis of \(V\) has the same number of elements; that number is the dimension of \(V\).

Check yourself
Are \(\vv_1 = \begin{bmatrix}1\\2\\3\end{bmatrix}\), \(\vv_2 = \begin{bmatrix}2\\4\\6\end{bmatrix}\), \(\vv_3 = \begin{bmatrix}0\\1\\1\end{bmatrix}\) linearly independent? What is their span?
Correct. Correct on both counts. \(2\vv_1 - \vv_2 + 0\vv_3 = \vzero\) is a non-trivial combination giving zero, which is exactly the definition of dependence. Since \(\vv_2\) adds nothing that \(\vv_1\) did not already provide, the span is \(\operatorname{span}\{\vv_1,\vv_3\}\) — and those two are independent (neither is a multiple of the other), so the span is a 2-dimensional plane through the origin.

Three vectors in \(\RR^3\) that only span a plane means any matrix built from them has rank 2, not 3, and is therefore singular. This is exactly what happens when a dataset contains a duplicated or perfectly-correlated feature — say, one column in metres and another in centimetres.
Not quite. Being different is not enough — independence is about whether any one is reachable from the others. Here \(\vv_2\) is literally \(2\vv_1\), so it lies on the same line through the origin and contributes no new direction. Test independence by solving \(\sum\alpha_i\vv_i=\vzero\), not by inspecting for duplicates.
Not quite. The dependence part is right, but only \(\vv_2\) is redundant. \(\vv_3=(0,1,1)\) is not a multiple of \(\vv_1=(1,2,3)\) — the first coordinate would force the scalar to be 0, which fails elsewhere. So two independent directions survive and the span is a plane, not a line.

4The dot product and what it measures

Definition — dot product

For \(\vu,\vv\in\RR^d\),

\[ \vu\cdot\vv \;=\; \vu^\top\vv \;=\; \sum_{i=1}^{d}u_iv_i. \]

The Euclidean norm is \(\|\vu\| = \sqrt{\vu\cdot\vu} = \sqrt{\sum_i u_i^2}\), which is Pythagoras in \(d\) dimensions.

Algebraically that is a sum of products, and it is not obvious it means anything. The following theorem says it means everything: it encodes both length and angle.

Deriving u·v = ‖u‖‖v‖cos θ from the law of cosines0 / 5
\[\|\vu-\vv\|^2 = \|\vu\|^2 + \|\vv\|^2 - 2\|\vu\|\,\|\vv\|\cos\theta\]
The law of cosines. Pure geometry, no dot products involved: in the triangle with sides \(\vu\), \(\vv\) and \(\vu-\vv\), with \(\theta\) the angle between \(\vu\) and \(\vv\). It generalises Pythagoras, which is the special case \(\theta=90^\circ\).
\[\|\vu-\vv\|^2 = (\vu-\vv)\cdot(\vu-\vv)\]
Algebra side, step 1. By definition of the norm. Now we expand this using only the definition of the dot product as a sum.
\[= \vu\cdot\vu - \vu\cdot\vv - \vv\cdot\vu + \vv\cdot\vv = \|\vu\|^2 - 2\,\vu\cdot\vv + \|\vv\|^2\]
Expand. The dot product distributes over addition, because \(\sum_i(u_i-v_i)(u_i-v_i)\) expands term by term. And it is symmetric, \(\vu\cdot\vv=\vv\cdot\vu\), since \(u_iv_i=v_iu_i\). Both facts are immediate from the coordinate definition.
\[\|\vu\|^2 - 2\,\vu\cdot\vv + \|\vv\|^2 = \|\vu\|^2+\|\vv\|^2-2\|\vu\|\|\vv\|\cos\theta\]
Equate the two expressions. Both compute \(\|\vu-\vv\|^2\), so they are equal. The \(\|\vu\|^2\) and \(\|\vv\|^2\) terms cancel from both sides.
\[\boxed{\;\vu\cdot\vv = \|\vu\|\,\|\vv\|\cos\theta\;}\]
Divide by \(-2\). The algebraic definition and the geometric one coincide. Immediate consequences: \(\vu\cdot\vv=0\) exactly when \(\cos\theta=0\), i.e. the vectors are perpendicular; the dot product is positive when the angle is acute and negative when obtuse. In \(\RR^{784}\) you cannot see the angle, but this formula still defines it.
Dot product, angle, and projectionInteractive
uvproj of v onto uresidual (⊥ to u)
50
2.40
2.00
The green vector is the shadow v casts on the line through u. The pink residual joins the tip of v to that shadow, and it is always perpendicular to u — check the readout, the residual's dot product with u is zero to machine precision. Rotate v past 90 degrees and the dot product goes negative, and the projection flips to the other side of the origin.

5Projection: the single most useful formula in the course

Given \(\vv\), what is the closest point to it on the line through \(\vu\)? Answering that gives least squares, PCA, Fourier analysis and Gram–Schmidt. Let us derive it two ways, because they give different insights.

The projection formula, derived by orthogonality0 / 5
\[\vp = k\,\vu \quad\text{for some scalar } k\]
Parameterise. Any point on the line through \(\vu\) (and the origin) is a scalar multiple of \(\vu\). So the whole problem is to find one number, \(k\).
\[\vr = \vv - \vp = \vv - k\vu \qquad\text{(the residual)}\]
Name the error. The residual is what is left over. Intuition: the closest point should make the error perpendicular to the line — if the error had any component along \(\vu\), you could slide \(\vp\) in that direction and do better.
\[\vr\cdot\vu = 0 \quad\Longrightarrow\quad (\vv-k\vu)\cdot\vu = 0\]
Impose orthogonality. This is the modelling step, and it is exactly the intuition made algebraic. It is called the normal equation, and in Lecture 10 the matrix version of this single line becomes \(\mX^\top(\vy - \mX\vw)=\vzero\).
\[\vv\cdot\vu - k\,(\vu\cdot\vu) = 0 \quad\Longrightarrow\quad k = \frac{\vu\cdot\vv}{\vu\cdot\vu} = \frac{\vu\cdot\vv}{\|\vu\|^2}\]
Solve for k. Distribute and rearrange. Division is legal as long as \(\vu\neq\vzero\).
\[\boxed{\;\operatorname{proj}_{\vu}(\vv) = \frac{\vu\cdot\vv}{\|\vu\|^{2}}\,\vu \;=\; \left(\hat{\vu}\cdot\vv\right)\hat{\vu}, \quad \hat{\vu}=\frac{\vu}{\|\vu\|}\;}\]
The formula. The second form is worth internalising: project by dotting with the unit vector, then scaling that unit vector by the result. If \(\vu\) is already a unit vector the whole thing is just \((\vu\cdot\vv)\vu\), which is why orthonormal bases are so pleasant to work with. Compare with Lecture 7's least-squares answer \(\hat w = \frac{\sum x_iy_i}{\sum x_i^2}\) — it is literally this formula, with \(\vu=\vx\) and \(\vv=\vy\). The one-parameter regression was a projection.
The other derivation — try it

Derive the same formula by minimising instead of imposing orthogonality. Define

\[g(k) = \|\vv - k\vu\|^2\]

and minimise it over \(k\) using Lecture 7's machinery. You should get the same \(k\), and you should also be able to confirm it is a minimum rather than a maximum.

Expand using bilinearity of the dot product:

\[ g(k) = (\vv-k\vu)\cdot(\vv-k\vu) = \|\vv\|^2 - 2k\,(\vu\cdot\vv) + k^2\|\vu\|^2. \]

This is a quadratic in \(k\) — the norms and the dot product are just numbers. Differentiate:

\[ g'(k) = -2(\vu\cdot\vv) + 2k\|\vu\|^2 \;\stackrel{!}{=}\;0 \quad\Longrightarrow\quad k = \frac{\vu\cdot\vv}{\|\vu\|^2}. \]

Same answer ✓. And \(g''(k)=2\|\vu\|^2>0\) whenever \(\vu\neq\vzero\), so \(g\) is strictly convex and this critical point is the unique global minimum, by Lecture 7's convexity theorem.

Why two derivations matter. The orthogonality route says the best approximation leaves a perpendicular error — a geometric fact you can see. The minimisation route says the best approximation minimises squared distance — an optimisation fact you can compute. They are the same theorem, and the equivalence is precisely why least squares has a clean geometric interpretation. Every appearance of \((\mX^\top\mX)^{-1}\mX^\top\) in statistics is this equivalence in matrix form.

6Norms: more than one notion of length

The Euclidean norm is one choice among a family:

\[\|\vx\|_p = \left(\sum_{i=1}^{d}|x_i|^p\right)^{1/p}, \qquad p\ge 1; \qquad \|\vx\|_\infty = \max_i |x_i|.\]

Unit balls {x : ‖x‖ₚ = 1} — the shape of a regulariserInteractive
‖x‖ₚ = 1a level set of the data-fit losswhere they first touch
2.00
At p = 1 the ball is a diamond with sharp corners ON THE AXES. At p = 2 it is a smooth circle. Now watch the green contact point: the purple ellipse is a loss contour being inflated until it first touches the ball, which is what constrained regularisation does. For p = 1 the touch happens at a corner, where one coordinate is exactly ZERO — that is why L1 produces sparse solutions. For p = 2 the touch is at a generic point with both coordinates nonzero. The geometry IS the explanation.
Where this shows up in ML

Cosine similarity. Rearranging the angle formula,

\[\cos\theta = \frac{\vu\cdot\vv}{\|\vu\|\,\|\vv\|} = \hat{\vu}\cdot\hat{\vv} \in[-1,1].\]

This is how similarity between embeddings is measured in essentially every retrieval, recommendation and RAG system. Two reasons it is preferred over raw Euclidean distance:

Vectors in NumPy — and why you should never write the loopPython · NumPy

7Exercises

★

Let \(\vu=(2,-1,3)\), \(\vv=(1,4,-2)\).

  1. Compute \(\vu\cdot\vv\), \(\|\vu\|\), \(\|\vv\|\), and the angle between them.
  2. Find \(\operatorname{proj}_{\vu}(\vv)\) and verify the residual is perpendicular to \(\vu\).
  3. Find a nonzero vector perpendicular to both.

1. \(\vu\cdot\vv = 2(1)+(-1)(4)+3(-2) = 2-4-6 = -8\).
\(\|\vu\|=\sqrt{4+1+9}=\sqrt{14}\approx3.7417\), \(\|\vv\|=\sqrt{1+16+4}=\sqrt{21}\approx4.5826\).
\(\cos\theta = \frac{-8}{\sqrt{14}\sqrt{21}} = \frac{-8}{\sqrt{294}} \approx -0.46657\), so \(\theta\approx 117.8^\circ\). Obtuse, consistent with the negative dot product.

2. \(k = \frac{\vu\cdot\vv}{\|\vu\|^2} = \frac{-8}{14} = -\frac47\).
\(\operatorname{proj}_{\vu}(\vv) = -\frac47(2,-1,3) = \left(-\frac87,\frac47,-\frac{12}{7}\right) \approx(-1.1429, 0.5714, -1.7143)\).
Residual \(\vr = \vv - \vp = \left(1+\frac87,\ 4-\frac47,\ -2+\frac{12}{7}\right) = \left(\frac{15}{7},\frac{24}{7},-\frac27\right)\).
Check: \(\vr\cdot\vu = \frac{30}{7}-\frac{24}{7}-\frac{6}{7} = 0\) ✓.

3. The cross product works in \(\RR^3\): \(\vu\times\vv = \big((-1)(-2)-3(4),\ 3(1)-2(-2),\ 2(4)-(-1)(1)\big) = (2-12,\ 3+4,\ 8+1) = (-10,7,9)\).
Check: \((-10)(2)+7(-1)+9(3) = -20-7+27=0\) ✓ and \((-10)(1)+7(4)+9(-2) = -10+28-18=0\) ✓.
Alternatively, without the cross product: solve \(2a-b+3c=0\) and \(a+4b-2c=0\). Two equations, three unknowns, so a one-dimensional family of solutions — which is what "perpendicular to a plane" means.

★★

Prove the Cauchy–Schwarz inequality:

\[ |\vu\cdot\vv| \le \|\vu\|\,\|\vv\| \quad\text{for all }\vu,\vv\in\RR^d,\]

with equality if and only if \(\vu\) and \(\vv\) are parallel.

Hint: consider \(g(t)=\|\vv-t\vu\|^2\ge0\) and use the fact that a non-negative quadratic has non-positive discriminant.

Assume \(\vu\neq\vzero\) (if \(\vu=\vzero\) both sides are 0 and the inequality holds trivially).

Define \(g(t)=\|\vv-t\vu\|^2\). A squared norm is a sum of squares, so \(g(t)\ge0\) for every real \(t\). Expanding as in the projection derivation:

\[g(t) = \|\vu\|^2 t^2 - 2(\vu\cdot\vv)\,t + \|\vv\|^2 .\]

This is a quadratic \(at^2+bt+c\) with \(a=\|\vu\|^2>0\), \(b=-2(\vu\cdot\vv)\), \(c=\|\vv\|^2\).

An upward parabola that is never negative has at most one real root, so its discriminant satisfies \(b^2-4ac\le 0\):

\[4(\vu\cdot\vv)^2 - 4\|\vu\|^2\|\vv\|^2 \le 0 \quad\Longrightarrow\quad (\vu\cdot\vv)^2 \le \|\vu\|^2\|\vv\|^2.\]

Taking non-negative square roots gives \(|\vu\cdot\vv|\le\|\vu\|\|\vv\|\). ∎

Equality. Equality in the discriminant means the parabola has exactly one real root \(t_0\), i.e. \(g(t_0)=\|\vv-t_0\vu\|^2=0\), i.e. \(\vv=t_0\vu\) — the vectors are parallel. Conversely if \(\vv=t_0\vu\) then \(|\vu\cdot\vv|=|t_0|\|\vu\|^2=\|\vu\|\|\vv\|\). ∎

Why it matters. Cauchy–Schwarz is what guarantees \(\cos\theta\in[-1,1]\), so the angle is well defined in any dimension. It also underlies the triangle inequality, the fact that correlation lies in \([-1,1]\), and Chebyshev's inequality in Lecture 14.

★★

The vectors \(\vv_1=(1,1,0)\), \(\vv_2=(1,0,1)\), \(\vv_3=(0,1,1)\).

  1. Are they linearly independent? Prove your answer by solving \(\sum\alpha_i\vv_i=\vzero\).
  2. Is \((2,3,4)\) in their span? If so, find the coefficients.
  3. What is \(\operatorname{span}\{\vv_1,\vv_2,\vv_3\}\)?

1. \(\alpha_1(1,1,0)+\alpha_2(1,0,1)+\alpha_3(0,1,1)=(0,0,0)\) gives

\[\alpha_1+\alpha_2=0,\qquad \alpha_1+\alpha_3=0,\qquad \alpha_2+\alpha_3=0.\]

From the first two, \(\alpha_2=-\alpha_1\) and \(\alpha_3=-\alpha_1\). Substituting into the third: \(-2\alpha_1=0\), so \(\alpha_1=0\), and then \(\alpha_2=\alpha_3=0\). Only the trivial solution → independent ✓.

2. Solve \(\alpha_1+\alpha_2=2\), \(\alpha_1+\alpha_3=3\), \(\alpha_2+\alpha_3=4\). Add all three: \(2(\alpha_1+\alpha_2+\alpha_3)=9\), so the total is \(4.5\). Subtracting each original equation in turn: \(\alpha_3=4.5-2=2.5\), \(\alpha_2=4.5-3=1.5\), \(\alpha_1=4.5-4=0.5\).
Check: \(0.5(1,1,0)+1.5(1,0,1)+2.5(0,1,1)=(0.5+1.5,\ 0.5+2.5,\ 1.5+2.5)=(2,3,4)\) ✓.

3. Three independent vectors in \(\RR^3\) span a 3-dimensional subspace of \(\RR^3\), which must be all of \(\RR^3\). So they form a basis — and part 2 was guaranteed to succeed before we did any arithmetic. Note also that the solution in part 2 was unique, which is exactly what independence buys you: a basis gives every vector exactly one set of coordinates.

★★★

Coding + geometry. The curse of dimensionality, measured.

  1. Sample 1000 pairs of random vectors from the standard Gaussian in \(\RR^d\), for \(d\in\{2,10,100,1000,10000\}\). Compute the distribution of the angle between each pair. What happens as \(d\) grows, and why is it surprising?
  2. Prove the trend: show \(\E[\hat{\vu}\cdot\hat{\vv}]=0\) and argue the variance shrinks like \(1/d\).
  3. What does this imply for cosine similarity as a retrieval signal in high dimensions?
import numpy as np
rng = np.random.default_rng(0)
for d in (2, 10, 100, 1000, 10000):
    u = rng.normal(size=(1000, d)); v = rng.normal(size=(1000, d))
    u /= np.linalg.norm(u, axis=1, keepdims=True)
    v /= np.linalg.norm(v, axis=1, keepdims=True)
    cos = np.sum(u*v, axis=1)
    ang = np.degrees(np.arccos(np.clip(cos, -1, 1)))
    print(f"d={d:6d}  mean angle {ang.mean():6.2f} deg   sd {ang.std():5.2f}"
          f"   mean|cos| {np.abs(cos).mean():.4f}   1/sqrt(d) {1/np.sqrt(d):.4f}")

1. The mean angle sits at \(90^\circ\) for every \(d\), but the spread collapses: at \(d=2\) the standard deviation is about \(52^\circ\), at \(d=100\) about \(5.7^\circ\), at \(d=10000\) about \(0.57^\circ\). In high dimensions two random vectors are almost exactly orthogonal, essentially always.

That is surprising because in the plane, random vectors are as likely to be nearly parallel as nearly perpendicular. High-dimensional space has vastly more "sideways" directions than "along" ones, and almost all of the volume sits near the equator relative to any fixed axis.

2. Write \(\hat{\vu}\cdot\hat{\vv}=\sum_i \hat u_i\hat v_i\). Since \(\vu\) and \(\vv\) are independent and each is symmetric about the origin, \(\E[\hat u_i\hat v_i]=\E[\hat u_i]\E[\hat v_i]=0\), so \(\E[\hat{\vu}\cdot\hat{\vv}]=0\) by linearity of expectation (Lecture 13).

For the variance: condition on \(\hat{\vu}\). By rotational symmetry of the Gaussian we may take \(\hat{\vu}=\ve_1\), so \(\hat{\vu}\cdot\hat{\vv}=\hat v_1\), the first coordinate of a uniformly random unit vector. By symmetry \(\E[\hat v_i^2]\) is the same for all \(i\), and \(\sum_i \hat v_i^2 = 1\), so \(\E[\hat v_1^2]=\frac1d\). Hence \(\Var(\hat{\vu}\cdot\hat{\vv})=\frac1d\) and the standard deviation is \(1/\sqrt d\) ✓ — which is exactly the last column your code prints, matching \(\E|\cos|\) up to the constant \(\sqrt{2/\pi}\approx0.8\).

3. Two consequences pulling in opposite directions.

Good news: a high-dimensional space has room for enormous numbers of nearly-orthogonal directions, so unrelated concepts can be given essentially non-interfering embeddings. This is why embedding dimensions of 768 or 1536 work at all, and it is the intuition behind the Johnson–Lindenstrauss lemma.

Bad news: the baseline similarity between unrelated items concentrates tightly around 0, so the entire useful signal lives in a narrow band, and small numerical or estimation errors eat a large fraction of it. It also means a raw cosine value is uninterpretable without a reference distribution — 0.3 might be a strong match in one embedding space and noise in another. This is why practical retrieval systems compare relative rankings rather than absolute thresholds, and why re-ranking models exist.


Next: vectors are the data; matrices are what we do to it. Lecture 9 derives matrix multiplication from the requirement that it represent composition of linear maps — which explains, among other things, why the dimensions have to line up the way they do.