Introduction

Welcome to the fascinating world of Locally Linear Embedding (LLE), a vital tool in our dimensionality reduction toolbox. Unlike linear techniques like Principal Component Analysis (PCA), LLE shines at preserving local neighborhood structure in high-dimensional data.

In this lesson, we’ll unpack the LLE algorithm, discuss when to use it, and contrast it with PCA. We’ll implement it in R, using ggplot2 for visualization and a small helper we’ll write to run LLE.

What is Locally Linear Embedding and its Use Cases?

LLE preserves relationships within local neighborhoods while reducing dimensionality, capturing twists and turns in non-linear manifolds (e.g., images, pose, genomics). Like reading a street map vs. a bird’s-eye projection: PCA may distort local distances, whereas LLE maintains them.

Understanding the Theory Behind LLE

LLE solves two coupled problems:

Step 1: Reconstruct each point from its K nearest neighbors

minW  i=1NxijN(i)Wijxj2s.t.jN(i)Wij=1\min_{W}\;\sum_{i=1}^{N}\left\|x_i-\sum_{j\in N(i)} W_{ij}x_j\right\|^2 \quad\text{s.t.}\quad \sum_{j\in N(i)}W_{ij}=1
Breaking down the LLE Algorithm: Generating the Data

We’ll use the Swiss Roll—a classic 3D manifold that’s hard for linear methods but perfect for LLE.

Applying Locally Linear Embedding (in R)

Below is a minimal LLE implementation in pure R. It computes K-NN by brute force (fine for ~1–2k points), solves local weight systems with a tiny regularization, and then embeds via the eigenvectors of the symmetric matrix M=(IW)(IW)M=(I-W)^\top(I-W).

Applying PCA for Comparison
Data Visualization

We’ll compare the original Swiss Roll projection (x vs z) with LLE and PCA in 2D.

You should see LLE unfold the roll into a smooth 2D strip, while PCA tends to smear/overlap the manifold because it’s linear. Look at example below:

Comparing Reconstruction Errors

We can compute the LLE reconstruction error directly from the weights (same objective as Step 1) and the PCA reconstruction error as the fraction of variance not explained by the first two PCs.

(Exact values vary with noise and sample size, but LLE should be very low; PCA typically leaves notable error on Swiss Roll.)

Lesson Summary and Practice

Congratulations! You’ve explored Locally Linear Embedding (LLE) and contrasted it with PCA using the Swiss Roll dataset. You saw how LLE preserves local neighborhood structure and effectively unfolds a non-linear manifold into 2D, while PCA—being linear—cannot capture these curved relationships.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal