Principal Component Analysis

Principal component analysis (PCA) is a linear dimensionality reduction technique.

Advantages of PCA / SVD

  • State of the art for many applications (supervised and unsupervised)
  • Incredibly efficient (often, almost linear time)
  • Strong theoretical background
  • Can also be used to store data in more efficient way (image compression)
  • Visual evaluation possible for small number of components (2 or 3)
Link to original

Abstract

PCA and SVD (singular value decomposition) are slightly different but similar. We look at PCA in this course (which often uses SVD).

Example

Say you have a bunch of house listings and you would like to group them into: student housing, regular, luxury

Let’s assume we have the features: floor size (), no. of rooms, distance to supermarket, distance to King’s, “hipster vibe”.

We want to reduce this to two or three features, we can do it like this:

  • Size (floor size, no. of rooms)
  • Location (distance to supermarket & King’s, “hipster vibe”)

The floor size and number of rooms are often correlated:

We can take the line that minimises the Least Square Distance, which gives us the following projection of points:

We want to maximise the variance which helps retain more information.

3D to 2D

Let’s say we have our three dimensions as relating to location (on l.h.s):

We can reduce it to 2D so it looks like the r.h.s. We may wish to reduce it to just a line (1D) but this can be very lossy.

5D to 3D

If we plot our 5D data using the components we found (1 for size and 2 for location), we get this 3D plot: (we can see the classes are well-separated)

We’ve reduced the information but retained important factors.

Matrices

Matrix Multiplication

Link to original

Matrix Transpose

The transpose of a matrix is an operation which flips the matrix along its diagonal.

Link to original

Transformation Matrix Cheatsheet

Matrix Stretch Transformation

We can stretch along the -axis and squish along the -axis with the following matrix:

Link to original

Largest eigenvalue is and second largest is .

The corresponding eigenvectors are and .

Matrix Rotation Transformation

We can rotate counterclockwise:

Link to original

Matrix Shear Mapping Transformation

Shear mapping (let for the example):

Link to original

Eigenvector

Eigenvector

When a vector doesn’t change its direction after multiplying with a matrix, then it’s an eigenvector.

Formally, a vector is an eigenvector of the matrix if where is the corresponding eigenvalue.

For example, does not change when shear mapping.

Link to original

Eigenvalue

In the stretching example earlier, both vectors were eigenvectors as they didn’t change direction, however they did change length: the value by which the length changes is called the eigenvalue . (values provided in example above as a note)

Link to original

Consider

We can verify that and :

The second eigenvector is and we want to find so we plug it into the formula.

PCA Algorithm

PCA Algorithm

Let’s say this is our data matrix (e.g. our houses), where each data point is a -dimensional row vector.

  1. Compute the mean row vector
  2. Compute the mean row matrix:

\bar X = \begin{pmatrix} 1 \ 1 \ \vdots \ 1 \end{pmatrix} \cdot \bar x^T = \begin{pmatrix} - & \bar x^T & - \ - & \bar x^T & - \ & \vdots \ - & \bar x^T & - \ \end{pmatrix} $$

The dimensions are . 3. Subtract mean (obtain mean centred data)

The dimensions are . 4. Compute the covariance matrix of rows of

The dimensions are . (more notes on why we do this below) 5. Compute the largest eigenvectors of C (not covered in module, use Python or WolframAlpha). Each eigenvector has dimensions .

Note

Depending on implementation, may not be sorted (e.g. certain Python libraries). Hence, sort eigenvectors by decreasing order of eigenvalues.

  1. Compute matrix of -largest eigenvectors. Dimensions of are .
  2. Multiply each datapoint for with . Dimensions of are

We’ve now reduced the number of dimensions from to .

Link to original

Why do we compute the covariance matrix?

The covariance matrix measures the correlation between pairs of features. By finding the largest eigenvectors, we can explain most of the variance in data.

The more variance is explained by the eigenvectors, the more important they are.

We can measure the explained variance by considering the quantity:

0 items under this folder.