Linear Transformations

Linear transformations are a concept in linear algebra that explain how data points move and interact in space.

In this lesson, we’ll explore linear transformations step by step.

This Lesson Will Cover
  • βœ–οΈ Matrix multiplication
  • πŸ“ Projections
  • πŸ”’ Eigenvalues and eigenvectors
  • πŸ”„ Change of basis
  • πŸ“Š Principal Component Analysis (PCA)
Linear Transformations Topics for ML
Linear transformations topics for ML.

Matrix Multiplication

We already talked about matrix multiplication in the previous lesson, but it’s worth revisiting here because it can be pretty confusing.

We’ll start with the case where one of the matrices is 1D (a vector).

Matrix-vector Multiplication

When we multiply a matrix by a vector, the result is a transformed version of the original vector.

For a matrix A\mathbf{A} and a vector x\mathbf{x}:

y=Aβ‹…x \mathbf{y} = \mathbf{A} \cdot \mathbf{x}

For example, here’a a 2 x 2 matrix multiplied by a 2 x 1 matrix (a vector):

A=[2003],x=[14] \mathbf{A} = \begin{bmatrix} 2 & 0 \\ 0 & 3 \end{bmatrix}, \quad \mathbf{x} = \begin{bmatrix} 1 \\ 4 \end{bmatrix}
y=Aβ‹…x=[2β‹…1+0β‹…40β‹…1+3β‹…4]=[212] \mathbf{y} = \mathbf{A} \cdot \mathbf{x} = \begin{bmatrix} 2 \cdot 1 + 0 \cdot 4 \\ 0 \cdot 1 + 3 \cdot 4 \end{bmatrix} = \begin{bmatrix} 2 \\ 12 \end{bmatrix}

Matrix-matrix Multiplication

This is just a higher-dimensional extension of matrix-vector multiplication, where we multiply two matrices together to create a new matrix.

If A\mathbf{A} is an mΓ—nm \times n matrix and B\mathbf{B} is an nΓ—pn \times p matrix, their product C=Aβ‹…B\mathbf{C} = \mathbf{A} \cdot \mathbf{B} is an mΓ—pm \times p matrix:

Cij=βˆ‘k=1nAikβ‹…Bkj \mathbf{C}_{ij} = \sum_{k=1}^{n} \mathbf{A}_{ik} \cdot \mathbf{B}_{kj}

For example, here’s a 2 x 3 matrix multiplied by a 3 x 4 matrix:

A=[123456],B=[123456789101112] \mathbf{A} = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}, \quad \mathbf{B} = \begin{bmatrix} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & 10 & 11 & 12 \end{bmatrix}
C=[(1β‹…1+2β‹…5+3β‹…9)(1β‹…2+2β‹…6+3β‹…10)(1β‹…3+2β‹…7+3β‹…11)(1β‹…4+2β‹…8+3β‹…12)(4β‹…1+5β‹…5+6β‹…9)(4β‹…2+5β‹…6+6β‹…10)(4β‹…3+5β‹…7+6β‹…11)(4β‹…4+5β‹…8+6β‹…12)]=[3844505689104119134] \mathbf{C} = \begin{bmatrix} (1 \cdot 1 + 2 \cdot 5 + 3 \cdot 9) & (1 \cdot 2 + 2 \cdot 6 + 3 \cdot 10) & (1 \cdot 3 + 2 \cdot 7 + 3 \cdot 11) & (1 \cdot 4 + 2 \cdot 8 + 3 \cdot 12) \\ (4 \cdot 1 + 5 \cdot 5 + 6 \cdot 9) & (4 \cdot 2 + 5 \cdot 6 + 6 \cdot 10) & (4 \cdot 3 + 5 \cdot 7 + 6 \cdot 11) & (4 \cdot 4 + 5 \cdot 8 + 6 \cdot 12) \end{bmatrix} \\ = \begin{bmatrix} 38 & 44 & 50 & 56 \\ 89 & 104 & 119 & 134 \end{bmatrix}
Example: Neural Network Forward Pass

In a neural network, each layer performs a linear transformation of its input.

Consider a layer with 6 nodes feeding forward into a layer with 2 nodes:

Input x=[x1x2x3x4x5x6], Weights W=[w11w12w21w22w31w32w41w42w51w52w61w62] \text{Input } \mathbf{x} = \begin{bmatrix} x_1 \\ x_2 \\ x_3 \\ x_4 \\ x_5 \\ x_6 \end{bmatrix}, \text{ Weights } \mathbf{W} = \begin{bmatrix} w_{11} & w_{12} \\ w_{21} & w_{22} \\ w_{31} & w_{32} \\ w_{41} & w_{42} \\ w_{51} & w_{52} \\ w_{61} & w_{62} \end{bmatrix}

The layer’s output is computed as:

y=WTβ‹…x \mathbf{y} = \mathbf{W}^T \cdot \mathbf{x}

Let’s plug in some values and see how this works out:

x=[123456],W=[0.10.40.20.50.30.60.10.20.20.30.30.4] \mathbf{x} = \begin{bmatrix} 1 \\ 2 \\ 3 \\ 4 \\ 5 \\ 6 \end{bmatrix}, \mathbf{W} = \begin{bmatrix} 0.1 & 0.4 \\ 0.2 & 0.5 \\ 0.3 & 0.6 \\ 0.1 & 0.2 \\ 0.2 & 0.3 \\ 0.3 & 0.4 \end{bmatrix}
y=[(0.1β‹…1+0.2β‹…2+0.3β‹…3+0.1β‹…4+0.2β‹…5+0.3β‹…6)(0.4β‹…1+0.5β‹…2+0.6β‹…3+0.2β‹…4+0.3β‹…5+0.4β‹…6)]=[4.47.3] \mathbf{y} = \begin{bmatrix} (0.1 \cdot 1 + 0.2 \cdot 2 + 0.3 \cdot 3 + 0.1 \cdot 4 + 0.2 \cdot 5 + 0.3 \cdot 6) \\ (0.4 \cdot 1 + 0.5 \cdot 2 + 0.6 \cdot 3 + 0.2 \cdot 4 + 0.3 \cdot 5 + 0.4 \cdot 6) \end{bmatrix} = \begin{bmatrix} 4.4 \\ 7.3 \end{bmatrix}

As we can see, this transformation maps 6-dimensional input features to 2-dimensional outputs.

Projections

Projections are a particular type of linear transformation that maps a vector onto another vector or subspace.

Projecting a\mathbf{a} onto b\mathbf{b} means finding the shadow of a\mathbf{a} on b\mathbf{b}.

The formula for projecting a\mathbf{a} onto b\mathbf{b} is:

Projba=aβ‹…bbβ‹…bb=aβ‹…bβˆ₯bβˆ₯2b \text{Proj}_{\mathbf{b}} \mathbf{a} = \frac{\mathbf{a} \cdot \mathbf{b}}{\mathbf{b} \cdot \mathbf{b}} \mathbf{b} = \frac{\mathbf{a} \cdot \mathbf{b}}{\|\mathbf{b}\|^2} \mathbf{b}

For example:

If a=[3,4]\mathbf{a} = [3, 4] and b=[1,0]\mathbf{b} = [1, 0], then the projection of a\mathbf{a} on b\mathbf{b} is:

Projba=(3β‹…1+4β‹…0)(1β‹…1+0β‹…0)[1,0]=[3,0] \text{Proj}_{\mathbf{b}} \mathbf{a} = \frac{(3 \cdot 1 + 4 \cdot 0)}{(1 \cdot 1 + 0 \cdot 0)} [1, 0] = [3, 0]

We can picture what this looks like on a graph:

    y-axis
     ^
     |              * (a: 3, 4)
     |          .
     |      .
     |  .
-----+-------------------------------------> x-axis
     |   *          *
     |  (b: 1, 0)   (Proj_b(a): 3, 0)
     |

Projections are used all over the place. For example in linear regression.

Example: Solving for Linear Regression Coefficients with OLS

In linear regression, we model data as follows:

y=XΞ²+Ο΅ y = \mathbf{X}\boldsymbol{\beta} + \epsilon

Where:

  • yy is the target variable
  • X\mathbf{X} is the matrix of features
  • Ξ²\boldsymbol{\beta} are the coefficients we want to find
  • Ο΅\epsilon is the error term

We can solve for the coefficients Ξ²\boldsymbol{\beta} using OLS by minimizing the squared error between predictions and actual values. Let’s solve this with a simple dataset.

Given training data (4 records, 2 features):

x1 = [1, 2, 3, 4]
x2 = [2, 4, 5, 4]
y = [2.1, 3.8, 6.2, 7.9]

We need to solve for three coefficients (two features + the intercept term).

To find Ξ²\boldsymbol{\beta} using linear algebra, we use the normal equation: Ξ²=(XTX)βˆ’1XTy\boldsymbol{\beta} = (\mathbf{X}^T \mathbf{X})^{-1} \mathbf{X}^T y

I realize I have pulled this equation out of a hat. It can be derived by minimizing the mean squared error cost function.

First, let’s construct X\mathbf{X} by adding a column of 1’s for the intercept:

X=[112124135144] \mathbf{X} = \begin{bmatrix} 1 & 1 & 2 \\ 1 & 2 & 4 \\ 1 & 3 & 5 \\ 1 & 4 & 4 \end{bmatrix}

Now let’s compute XTX\mathbf{X}^T \mathbf{X}:

XTX=[111112342454][112124135144]=[41015103041154161] \mathbf{X}^T\mathbf{X} = \begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & 2 & 3 & 4 \\ 2 & 4 & 5 & 4 \end{bmatrix} \begin{bmatrix} 1 & 1 & 2 \\ 1 & 2 & 4 \\ 1 & 3 & 5 \\ 1 & 4 & 4 \end{bmatrix} = \begin{bmatrix} 4 & 10 & 15 \\ 10 & 30 & 41 \\ 15 & 41 & 61 \end{bmatrix}

Calculate (XTX)βˆ’1(\mathbf{X}^T \mathbf{X})^{-1}:

(XTX)βˆ’1=[2.93βˆ’1.370.21βˆ’1.370.68βˆ’0.100.21βˆ’0.100.02] (\mathbf{X}^T\mathbf{X})^{-1} = \begin{bmatrix} 2.93 & -1.37 & 0.21 \\ -1.37 & 0.68 & -0.10 \\ 0.21 & -0.10 & 0.02 \end{bmatrix}

Calculate XTy\mathbf{X}^T \mathbf{y}:

XTy=[111112342454][2.13.86.27.9]=[20.063.889.2] \mathbf{X}^T\mathbf{y} = \begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & 2 & 3 & 4 \\ 2 & 4 & 5 & 4 \end{bmatrix} \begin{bmatrix} 2.1 \\ 3.8 \\ 6.2 \\ 7.9 \end{bmatrix} = \begin{bmatrix} 20.0 \\ 63.8 \\ 89.2 \end{bmatrix}

Finally, calculate Ξ²=(XTX)βˆ’1XTy\boldsymbol{\beta} = (\mathbf{X}^T \mathbf{X})^{-1} \mathbf{X}^T \mathbf{y}:

Ξ²=[2.93βˆ’1.370.21βˆ’1.370.68βˆ’0.100.21βˆ’0.100.02][20.063.889.2]=[βˆ’0.821.850.15] \boldsymbol{\beta} = \begin{bmatrix} 2.93 & -1.37 & 0.21 \\ -1.37 & 0.68 & -0.10 \\ 0.21 & -0.10 & 0.02 \end{bmatrix} \begin{bmatrix} 20.0 \\ 63.8 \\ 89.2 \end{bmatrix} = \begin{bmatrix} -0.82 \\ 1.85 \\ 0.15 \end{bmatrix}

This gives us the equation y=βˆ’0.82+1.85x1+0.15x2y = -0.82 + 1.85x_1 + 0.15x_2, which minimizes the squared error for our data points.

We can verify this is a good fit:

  • When x1=1x_1 = 1, x2=2x_2 = 2: yβ‰ˆ1.48y \approx 1.48 (actual: 2.1)
  • When x1=2x_1 = 2, x2=4x_2 = 4: yβ‰ˆ3.78y \approx 3.78 (actual: 3.8)
  • When x1=3x_1 = 3, x2=5x_2 = 5: yβ‰ˆ5.93y \approx 5.93 (actual: 6.2)
  • When x1=4x_1 = 4, x2=4x_2 = 4: yβ‰ˆ7.63y \approx 7.63 (actual: 7.9)

One small but important point: this is not how linear model coefficients are actually solved when fitting on real training data. In practice, libraries like scikit-learn use numerical methods like gradient descent or QR decomposition, which are much faster and more stable.

In terms of speed, computing the matrix inverse like we saw above is O(nΒ³) time complexity for an nΓ—n matrix, making it impractical for large datasets.

In terms of stability, even tiny perturbations in the input data can cause large swings in the computed inverse, making the solution unreliable for practical applications. This instability is due to floating-point arithmetic limitations and rounding errors that get amplified during matrix operations. This is particularly problematic when computing inverses of matrices that are close to singular (meaning some features are highly correlated or redundant), as small rounding errors in the computation can lead to drastically different results.

Riiiight then. Computers make everything tricky, don’t they. Let’s return to the perfect world of linear algebra.

Eigenvalues and Eigenvectors

Eigenvalues and eigenvectors are crazy cool linear algebra constructs that have all sorts of applications including quantum mechanics.

An eigenvector is a vector that, when transformed by a matrix, only gets scaled (stretched or compressed) without changing direction.

For a matrix A\mathbf{A}, if vector v\mathbf{v} is an eigenvector with eigenvalue Ξ»\lambda, then:

Av=Ξ»v \mathbf{A}\mathbf{v} = \lambda\mathbf{v}

Let’s hold up for a second and think about this. Are we saying that A=Ξ»\mathbf{A} = \lambda? No we are not.

Think about what we just talked about - how eigenvectors behave with respect to a matrix multiplication: the result is a vector that points in the same direction, but which has been scaled up or down. (In general this is certainly not the case for matrix multiplicationβ€”just look at the examples from earlier in this lesson.)

So with this in mind, we can equivalently write the matrix multiplication Av\mathbf{A}\mathbf{v} as a scalar multiplication Ξ»v\lambda\mathbf{v}, i.e. multiplying each value in the eigenvector v\mathbf{v} by some constant Ξ»\lambda.

Let’s work through a simple example with a 2Γ—2 matrix:

A=[3113] \mathbf{A} = \begin{bmatrix} 3 & 1 \\ 1 & 3 \end{bmatrix}

To find eigenvalues, we solve what is known as the characteristic equation:

det(Aβˆ’Ξ»I)=0 det(\mathbf{A} - \lambda\mathbf{I}) = 0

Again, please excuse me for pulling this out of thin air. But just bear with me.

For our matrix:

det[3βˆ’Ξ»113βˆ’Ξ»]=(3βˆ’Ξ»)(3βˆ’Ξ»)βˆ’1=0 det\begin{bmatrix} 3-\lambda & 1 \\ 1 & 3-\lambda \end{bmatrix} = (3-\lambda)(3-\lambda) - 1 = 0
(3βˆ’Ξ»)2βˆ’1=0 (3-\lambda)^2 - 1 = 0
Ξ»2βˆ’6Ξ»+8=0 \lambda^2 - 6\lambda + 8 = 0

Solving this quadratic equation like we learned back in grade 8:

Ξ»=4 or Ξ»=2 \lambda = 4 \text{ or } \lambda = 2

For Ξ»=4\lambda = 4, we find eigenvector v1\mathbf{v}_1 by solving:

[3βˆ’4113βˆ’4]v1=[00] \begin{bmatrix} 3-4 & 1 \\ 1 & 3-4 \end{bmatrix}\mathbf{v}_1 = \begin{bmatrix} 0 \\ 0 \end{bmatrix}

This gives us v1=[11]\mathbf{v}_1 = \begin{bmatrix} 1 \\ 1 \end{bmatrix}

For Ξ»=2\lambda = 2, we get v2=[1βˆ’1]\mathbf{v}_2 = \begin{bmatrix} 1 \\ -1 \end{bmatrix}

Let’s visualize this result:

    y-axis
     ^
     |  v₁ (1,1)
     | /
     |/
-----+------------> x-axis
     |\
     | \
     |  vβ‚‚ (1,-1)
     |

And confirm that it satisfies our eigenvector condition:

Av=Ξ»v \mathbf{A}\mathbf{v} = \lambda\mathbf{v}

When we apply matrix A\mathbf{A} to v1\mathbf{v}_1:

Av1=[3113][11]=[44]=4[11] \mathbf{A}\mathbf{v}_1 = \begin{bmatrix} 3 & 1 \\ 1 & 3 \end{bmatrix}\begin{bmatrix} 1 \\ 1 \end{bmatrix} = \begin{bmatrix} 4 \\ 4 \end{bmatrix} = 4\begin{bmatrix} 1 \\ 1 \end{bmatrix}

The vector is scaled by 4 (the eigenvalue we calculated above) but maintains its direction!

Similarly for v2\mathbf{v}_2:

Av2=[3113][1βˆ’1]=[2βˆ’2]=2[1βˆ’1] \mathbf{A}\mathbf{v}_2 = \begin{bmatrix} 3 & 1 \\ 1 & 3 \end{bmatrix}\begin{bmatrix} 1 \\ -1 \end{bmatrix} = \begin{bmatrix} 2 \\ -2 \end{bmatrix} = 2\begin{bmatrix} 1 \\ -1 \end{bmatrix}

So what’s the significance of all this? Well, we’ll look at a concrete example of using eigenvectors for ML in the PCA example below. For now, consider that the matrix A\mathbf{A} could represent any kind of transformation on 2D data.

How could that look? Let’s take a vector v=[91]\mathbf{v} = \begin{bmatrix} 9 \\ 1 \end{bmatrix} and repeatedly apply our transformation matrix A\mathbf{A}:

First application:

Av=[3113][91]=[2812] \mathbf{A}\mathbf{v} = \begin{bmatrix} 3 & 1 \\ 1 & 3 \end{bmatrix}\begin{bmatrix} 9 \\ 1 \end{bmatrix} = \begin{bmatrix} 28 \\ 12 \end{bmatrix}

Second application:

A2v=[3113][2812]=[9664] \mathbf{A}^2\mathbf{v} = \begin{bmatrix} 3 & 1 \\ 1 & 3 \end{bmatrix}\begin{bmatrix} 28 \\ 12 \end{bmatrix} = \begin{bmatrix} 96 \\ 64 \end{bmatrix}

Third application:

A3v=[3113][9664]=[352256] \mathbf{A}^3\mathbf{v} = \begin{bmatrix} 3 & 1 \\ 1 & 3 \end{bmatrix}\begin{bmatrix} 96 \\ 64 \end{bmatrix} = \begin{bmatrix} 352 \\ 256 \end{bmatrix}

Notice how the vector is not only getting longer, but its direction is gradually aligning more and more with the eigenvector v1=[11]\mathbf{v}_1 = \begin{bmatrix} 1 \\ 1 \end{bmatrix}. We can see this by looking at the ratios:

  • Original vector: 19=0.111\frac{1}{9} = 0.111
  • After one application: 1228=0.429\frac{12}{28} = 0.429
  • After two applications: 6496=0.667\frac{64}{96} = 0.667
  • After three applications: 256352=0.727\frac{256}{352} = 0.727

The ratio is approaching 1, which means the vector is converging towards the direction of v1\mathbf{v}_1, our eigenvector with the largest eigenvalue!

The eigenvectors we found (v1\mathbf{v}_1 and v2\mathbf{v}_2) tell us the β€œnatural” directions in which A\mathbf{A} operates, and their eigenvalues (4 and 2) tell us the strength of that operation in each direction.

This decomposition into eigenvectors helps us understand exactly how A\mathbf{A} transforms spaceβ€”it stretches space by a factor of 4 in the direction [11]\begin{bmatrix} 1 \\ 1 \end{bmatrix} and by a factor of 2 in the direction [1βˆ’1]\begin{bmatrix} 1 \\ -1 \end{bmatrix}.

Change of Basis

What we just did - expressing a vector in terms of eigenvectors - is an example of a change of basis.

Instead of using the standard basis vectors [10]\begin{bmatrix} 1 \\ 0 \end{bmatrix} and [01]\begin{bmatrix} 0 \\ 1 \end{bmatrix}, we expressed our vector in terms of the eigenvectors.

The eigenvectors form what’s called an eigenbasis. When we use this basis, the transformation A\mathbf{A} becomes incredibly simpleβ€”it just scales each component by its corresponding eigenvalue. Let’s see this:

Original basis:

A[20]=[3113][20]=[62] \mathbf{A}\begin{bmatrix} 2 \\ 0 \end{bmatrix} = \begin{bmatrix} 3 & 1 \\ 1 & 3 \end{bmatrix}\begin{bmatrix} 2 \\ 0 \end{bmatrix} = \begin{bmatrix} 6 \\ 2 \end{bmatrix}

Eigenbasis:

[20]=1v1+1v2 \begin{bmatrix} 2 \\ 0 \end{bmatrix} = 1\mathbf{v}_1 + 1\mathbf{v}_2
A(1v1+1v2)=4(1v1)+2(1v2)=[62] \mathbf{A}(1\mathbf{v}_1 + 1\mathbf{v}_2) = 4(1\mathbf{v}_1) + 2(1\mathbf{v}_2) = \begin{bmatrix} 6 \\ 2 \end{bmatrix}

In the eigenbasis, the transformation matrix becomes diagonal:

A=[4002] \mathbf{A} = \begin{bmatrix} 4 & 0 \\ 0 & 2 \end{bmatrix}

This is why eigenvectors are so powerfulβ€”they reveal a coordinate system where complex transformations become simple scaling operations. The change of basis transforms our problem into a form that’s much easier to understand and work with.

Example: Principal Component Analysis (PCA)

PCA is a dimensionality reduction technique that finds the directions (principal components) along which data has maximum variance, allowing us to represent high-dimensional data using fewer dimensions while preserving the most important patterns and relationships.

Let’s work through PCA on a small dataset with 3 samples and 4 features:

X=[127489423458] \mathbf{X} = \begin{bmatrix} 1 & 2 & 7 & 4\\ 8 & 9 & 4 & 2\\ 3 & 4 & 5 & 8 \end{bmatrix}

Step 1: Center the data by subtracting the mean of each feature:

Mean values: ΞΌ1=4\mu_1 = 4, ΞΌ2=5\mu_2 = 5, ΞΌ3=163\mu_3 = \frac{16}{3}, ΞΌ4=143\mu_4 = \frac{14}{3}

Xc=[βˆ’3.00βˆ’3.001.670.334.004.00βˆ’1.33βˆ’2.67βˆ’1.00βˆ’1.00βˆ’0.332.33] \mathbf{X}_c = \begin{bmatrix} -3.00 & -3.00 & 1.67 & 0.33\\ 4.00 & 4.00 & -1.33 & -2.67\\ -1.00 & -1.00 & -0.33 & 2.33 \end{bmatrix}

Step 2: Calculate the covariance matrix:

C=1nβˆ’1XcTXc=[13.0013.00βˆ’7.33βˆ’8.6713.0013.00βˆ’7.33βˆ’8.67βˆ’7.33βˆ’7.334.563.11βˆ’8.67βˆ’8.673.116.22] \mathbf{C} = \frac{1}{n-1}\mathbf{X}_c^T\mathbf{X}_c = \begin{bmatrix} 13.00 & 13.00 & -7.33 & -8.67\\ 13.00 & 13.00 & -7.33 & -8.67\\ -7.33 & -7.33 & 4.56 & 3.11\\ -8.67 & -8.67 & 3.11 & 6.22 \end{bmatrix}

Step 3: Find eigenvalues and eigenvectors of the covariance matrix:

Solving det(Cβˆ’Ξ»I)=0det(\mathbf{C} - \lambda\mathbf{I}) = 0 gives us:

  • Ξ»1=32.44\lambda_1 = 32.44 (first eigenvalue)
  • Ξ»2=4.33\lambda_2 = 4.33 (second eigenvalue)
  • Ξ»3=0.01\lambda_3 = 0.01 (third eigenvalue)
  • Ξ»4=0.00\lambda_4 = 0.00 (fourth eigenvalue)

The corresponding eigenvectors are:

v1=[0.7070.707βˆ’0.408βˆ’0.500],v2=[0.0000.000βˆ’0.7070.707],v3=[βˆ’0.7070.7070.0000.000],v4=[0.0000.0000.5770.500] \mathbf{v}_1 = \begin{bmatrix} 0.707\\ 0.707\\ -0.408\\ -0.500 \end{bmatrix}, \mathbf{v}_2 = \begin{bmatrix} 0.000\\ 0.000\\ -0.707\\ 0.707 \end{bmatrix}, \mathbf{v}_3 = \begin{bmatrix} -0.707\\ 0.707\\ 0.000\\ 0.000 \end{bmatrix}, \mathbf{v}_4 = \begin{bmatrix} 0.000\\ 0.000\\ 0.577\\ 0.500 \end{bmatrix}

Step 4: Project the centered data onto the new basis:

P=Xc[0.7070.000βˆ’0.7070.0000.7070.0000.7070.000βˆ’0.408βˆ’0.7070.0000.577βˆ’0.5000.7070.0000.500]=[βˆ’5.232.830.000.007.31βˆ’1.410.000.00βˆ’2.08βˆ’1.410.000.00] \mathbf{P} = \mathbf{X}_c \begin{bmatrix} 0.707 & 0.000 & -0.707 & 0.000\\ 0.707 & 0.000 & 0.707 & 0.000\\ -0.408 & -0.707 & 0.000 & 0.577\\ -0.500 & 0.707 & 0.000 & 0.500 \end{bmatrix} = \begin{bmatrix} -5.23 & 2.83 & 0.00 & 0.00\\ 7.31 & -1.41 & 0.00 & 0.00\\ -2.08 & -1.41 & 0.00 & 0.00 \end{bmatrix}

This change of basis operation gives us our data in the new coordinate system. Notice how:

  • The first column corresponds to λ₁ = 32.44 (32.44/36.78 = 88.2% of total variance)
  • The second column corresponds to Ξ»β‚‚ = 4.33 (4.33/36.78 = 11.8% of total variance)
  • The remaining columns correspond to λ₃ = 0.01 and Ξ»β‚„ β‰ˆ 0.00 (together < 0.1% of total variance)

Here we are using the eigenvalues to explain the variance of our data, e.g. λ₁ + Ξ»β‚‚ + λ₃ + Ξ»β‚„ = 32.44 + 4.33 + 0.01 + 0.00 = 36.78

To reduce dimensionality, we can keep only the first two columns, giving us a 2D representation of our 4D data that preserves 99.8% of the variance:

Preduced=[βˆ’5.232.837.31βˆ’1.41βˆ’2.08βˆ’1.41] \mathbf{P}_{reduced} = \begin{bmatrix} -5.23 & 2.83\\ 7.31 & -1.41\\ -2.08 & -1.41 \end{bmatrix}
Why This Works

Remember our earlier discussion about change of basis?

PCA is finding a new coordinate system where:

  1. The axes are orthogonal (perpendicular to each other).
  2. The first axis points in the direction of maximum variance.
  3. Each subsequent axis points in the direction of maximum remaining variance.

The eigenvalues tell us how much variance is explained by each direction.

When we drop the components with small eigenvalues, we’re effectively removing the directions in which our data varies the least, thus preserving the most important patterns in the data.

This is why PCA is so powerful for problems that benefit from dimensionality reduction, like feature extraction, data visualization, and noise reduction.

Conclusion

That was totally awesome, wasn’t it?

How sad is it that I studied Physics for 6 years without having a clear conceptual understanding of what eigenvectors and eigenvalues are?

Just for fun, check this out. It’s the time-independent SchrΓΆdinger equation:

H^ψ=Eψ \hat{H}\psi = E\psi

Where:

  • H^\hat{H} is the Hamiltonian operator,
  • ψ\psi is the wavefunction,
  • EE is the energy.

Look familiar? This is an eigenvalue equation! The wavefunctions ψ\psi are eigenvectors of H^\hat{H} with eigenvalues EE.

But I digress…

I’d love to keep talking about linear algebra, but we’ve got too many other important topics to cover. So it’s time to leave linear algebra behindβ€”although (crucially) not forgotten.

Key Takeaways

πŸ”’ Matrix multiplication transforms vectors and matrices in predictable ways

πŸ“ Linear transformations like projections map data into new spaces

πŸ”„ Eigenvectors reveal natural directions of transformations

πŸ“Š Change of basis helps simplify complex transformations

πŸ’‘ These concepts enable powerful techniques like PCA and linear regression

Next: Probability and Statistics

Next up, we’ll learn about the most important concepts in probability and statistics for ML/AI.

Comments (0)

No comments yet.