Linear Algebra for Machine Learning

Change of Basis

Change of basis as re-expressing the same vector in a different coordinate system.

Lesson
7

Goal

Understand that a vector does not change when we change basis; only its coordinates do.

By the end of the lesson, you should be able to explain why orthonormal bases are especially convenient and why an orthogonal matrix can be inverted by transposition.

Motivation

The data usually does not care which coordinate system we pick.

We care.

When the coordinates are poorly chosen, a problem can look messy. When the coordinates line up with the geometry, the same problem can become almost trivial.

That is why basis changes matter in machine learning. PCA is one of the clearest examples: it looks for a better coordinate system, not a different dataset.

Prerequisites

Notation / Symbol Table

SymbolPlain-English nameMeaning
e1,e2e_1, e_2standard basis vectorsThe usual coordinate axes in R2\mathbb{R}^2.
u1,u2u_1, u_2alternate basis vectorsA different set of coordinate directions for the same space.
QQorthogonal matrixA matrix whose columns are orthonormal basis vectors.
QTQ^Ttranspose of QFor orthogonal QQ, the inverse transformation.
uvu \cdot vdot productA test for orthogonality.
IIidentity matrixThe matrix that leaves vectors unchanged.

Core Ideas

Coordinates are not the vector

If you write

v=[34],v = \begin{bmatrix} 3 \\ 4 \end{bmatrix},

those numbers are coordinates in the standard basis.

They are not properties of the vector itself.

If we change the basis, the coordinates change too, even though the vector is the same geometric object.

That distinction is the heart of the lesson.

A basis is a measuring system

The standard basis is

e1=[10],e2=[01].e_1 = \begin{bmatrix} 1 \\ 0 \end{bmatrix}, \qquad e_2 = \begin{bmatrix} 0 \\ 1 \end{bmatrix}.

Any vector in R2\mathbb{R}^2 can be written as a linear combination of those two directions.

But we could choose a different pair, such as

u1=[11],u2=[11].u_1 = \begin{bmatrix} 1 \\ 1 \end{bmatrix}, \qquad u_2 = \begin{bmatrix} 1 \\ -1 \end{bmatrix}.

If the two new directions are linearly independent, they also form a basis.

The vector did not change.

Only the coordinate system did.

Orthogonal and orthonormal bases

Two vectors are orthogonal if their dot product is zero:

uv=0.u \cdot v = 0.

If we also normalize them to have length 1, the basis is orthonormal.

Orthogonal bases are nice because the coordinate directions do not interfere with one another.

Orthonormal bases are even better because the algebra becomes especially clean.

Orthogonal matrices

If the columns of a matrix QQ form an orthonormal basis, then

QTQ=I.Q^TQ = I.

So

Q1=QT.Q^{-1} = Q^T.

That is a major simplification.

Instead of solving for an inverse with general linear algebra, we can just transpose.

Change of basis formula

If the columns of QQ are the new basis vectors, then the coordinates of a vector xx in that basis are

QTxQ^Tx

when QQ is orthogonal.

If the basis is not orthonormal, you still change coordinates, but you need the full inverse instead of the transpose.

That is why orthonormal bases are such a sweet spot.

Why this matters for PCA

Eigenvectors of a covariance matrix are perpendicular.

After normalization, they become an orthonormal basis.

If you place them into a matrix QQ, then QTxQ^Tx expresses each data point in a new coordinate system where the directions are uncoupled and ordered by variance.

That is the coordinate system PCA wants.

Worked Example

Let

Q=12[1111].Q = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}.

The columns of QQ are orthonormal, so Q1=QTQ^{-1} = Q^T.

Take

x=[34].x = \begin{bmatrix} 3 \\ 4 \end{bmatrix}.

Convert the coordinates:

QTx=12[1111][34]=12[71].Q^Tx = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} \begin{bmatrix} 3 \\ 4 \end{bmatrix} = \frac{1}{\sqrt{2}} \begin{bmatrix} 7 \\ -1 \end{bmatrix}.

So the same geometric vector now has coordinates

[7/21/2]\begin{bmatrix} 7 / \sqrt{2} \\ -1 / \sqrt{2} \end{bmatrix}

in the new basis.

Nothing moved in space.

Only the description changed.

If we want to recover the original coordinates, we multiply by QQ:

Q(QTx)=x.Q(Q^Tx) = x.

That is the benefit of orthogonality: the forward and backward conversions are cheap and exact in theory.

Why This Shows Up In ML

Machine learning is full of latent coordinate systems.

Feature spaces, principal components, embedding spaces, and orthogonal transforms all depend on the same idea:

Sometimes the smartest thing to do is not to change the data, but to change the coordinates.

Orthogonal transforms preserve lengths and angles, so they are numerically stable and geometrically honest.

That is one reason they are so useful in PCA, whitening, numerical linear algebra, and many optimization methods.

Common Confusions

Exercises

  1. Are these vectors orthogonal?

    [21],[12]\begin{bmatrix} 2 \\ 1 \end{bmatrix}, \quad \begin{bmatrix} 1 \\ -2 \end{bmatrix}
  2. Find the length of

    [34]\begin{bmatrix} 3 \\ 4 \end{bmatrix}

    and normalize it.

  3. Show that

    12[11]\frac{1}{\sqrt{2}} \begin{bmatrix} 1 \\ 1 \end{bmatrix}

    has length 1.

  4. Verify that

    Q=12[1111]Q = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}

    satisfies QTQ=IQ^TQ = I.

  5. Suppose your data point is

    x=[20].x = \begin{bmatrix} 2 \\ 0 \end{bmatrix}.

    Compute QTxQ^Tx using the same matrix QQ above, and interpret the resulting coordinates.

Implementation Exercise

Write a TypeScript helper that converts coordinates between the standard basis and an orthonormal basis.

A small API could look like this:

type Vector2 = [number, number];
type Matrix2x2 = [[number, number], [number, number]];

function multiplyMatrixVector(matrix: Matrix2x2, vector: Vector2): Vector2;
function transpose(matrix: Matrix2x2): Matrix2x2;

Then use those helpers to implement:

The goal is to make the coordinate change explicit instead of magical.

Reflection Questions

Next Lesson

Next: Projection.

The bridge is direct: once we know how to express vectors in a basis, we can ask for the closest vector in a subspace, which is projection.