Open In App

Locally Linear Embedding in machine learning

Last Updated : 23 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

LLE (Locally Linear Embedding) is a technique used to reduce the number of dimensions in a dataset without losing the important shape or structure of the data. It is an unsupervised method meaning it works without needing labeled data. LLE operates in several key steps:

  • First LLE finds the nearest neighbors of each point. Then it works out weights that show how each point relates to its neighbors. These weights help capture the local structure of the data.
  • Next LLE uses these weights to create a lower-dimensional version of the data. It keeps the important shape by picking the best directions (eigenvectors) based on the number of dimensions you want.

Consider a Swiss roll dataset a 3D shape that looks like a rolled-up sheet. Even though it’s curved and complex LLE can “unroll” it into a flat 2D shape while keeping the original structure and relationships between points.

Mathematical Implementation of LLE Algorithm

The main idea of LLE is that around each data point, the nearby points lie on a flat, simple surface. LLE tries to “unfold” the data while keeping these local flat relationships intact. Here’s a basic overview of how it works mathematically:

Minimize: \sum _{i} | x{_i} - \sum _{j} w _{ij} x{_j}|^2

Subject to : \sum {_j} w _{ij} = 1

Where:

  • xi represents the i-th data point.
  • wij are the weights that minimize the reconstruction error for data point xi using its neighbors.

Through this way helps to reduce number of dimensions while keeping important relationships in the data.

Working of Locally Linear Embedding Algorithm

The LLE algorithm can be broken down into several steps:

  • Neighborhood Selection: For each data point in the high-dimensional space LLE identifies its k-nearest neighbors. This step is important because LLE assumes that each data point can be well approximated by a linear combination of its neighbors.
  • Weight Matrix Construction: LLE computes a set of weights for each data point to express it as a linear combination of its neighbors. These weights are determined in such a way that the reconstruction error is minimized. Linear regression is often used to find these weights.
  • Global Structure Preservation: After constructing the weight matrix LLE aims to find a lower-dimensional representation of the data that best preserves the local linear relationships. It does this by seeking a set of coordinates in the lower-dimensional space for each data point that minimizes a cost function. This cost function evaluates how well each data point can be represented by its neighbors.
  • Output Embedding: Once the optimization process is complete LLE provides the final lower-dimensional representation of the data. This representation captures the essential structure of the data while reducing its dimensionality.

Parameters in LLE Algorithm

LLE has a few parameters that influence its behavior:

  • k (Number of Neighbors): It explain how to select the optimal number of neighbors and balance local and global relationships.
  • Dimensionality of Output Space: Discuss the importance of choosing the right dimensionality for the final representation.
  • Distance Metric: It introduce different distance metrics like Euclidean or Manhattan and explain their impact.
  • Regularization: If applicable briefly mention how regularization helps avoid overfitting.

Implementation of Locally Linear Embedding

1. Importing Libraries

First import necessary libraries including numpy, matplotlib.pyplot, make_swiss_roll from sklearn.datasets and LocallyLinearEmbedding from sklearn.manifold.

Python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_swiss_roll
from sklearn.manifold import LocallyLinearEmbedding

2. Generating a Synthetic Dataset (Swiss Roll)

It generates a synthetic dataset resembling a Swiss Roll using the make_swiss_roll function from scikit-learn.

  • n_samples specifies the number of data points to generate.
  • n_neighbors defines the number of neighbors used in the LLE algorithm.
Python
n_samples = 1000
n_neighbors = 10
X, _ = make_swiss_roll(n_samples=n_samples)

3. Applying Locally Linear Embedding (LLE)

An instance of the LLE algorithm is created with LocallyLinearEmbedding. The n_neighbors parameter determines the number of neighbors to consider during the embedding process. The LLE algorithm is then fitted to the original data X using the fit_transform method. This step reduces the dataset to two dimensions (n_components=2).

Python
lle = LocallyLinearEmbedding(n_neighbors=n_neighbors, n_components=2)
X_reduced = lle.fit_transform(X)

4. Visualizing the Original and Reduced Data

After reducing dimensions with LLE we plot the original high-dimensional data and the new lower-dimensional data. This helps us see if important patterns are preserved and easier to understand.

Python
plt.figure(figsize=(12, 6))

plt.subplot(121)
plt.scatter(X[:, 0], X[:, 1], c=X[:, 2], cmap=plt.cm.Spectral)
plt.title("Original Data")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")

plt.subplot(122)
plt.scatter(X_reduced[:, 0], X_reduced[:, 1], c=X[:, 2], cmap=plt.cm.Spectral)
plt.title("Reduced Data (LLE)")
plt.xlabel("Component 1")
plt.ylabel("Component 2")

plt.tight_layout()
plt.show()

Output:

Locally-Linear-Embedding
Locally Linear Embedding

In the second subplot the reduced data obtained from LLE (X_reduced) is visualized in a similar manner to the original data. The color of the data points is still determined by the third feature of the original data (X[:, 2]). The plt.tight_layout() function is used to ensure proper spacing between subplots.

Advantages of LLE

The dimensionality reduction method known as locally linear embedding (LLE) has many benefits for data processing and visualization. The following are LLE's main benefits:

  • Preservation of Local Structures: LLE is good at maintaining the in-data local relationships or structures. It captures the natural shape of the data by preserving the distances between points that are close to each other.
  • Handling Non-Linearity: It has the ability to capture nonlinear patterns and structures in the data unlike simple methods like PCA that only work well with straight-line patterns. This makes it useful for complex datasets.
  • Dimensionality Reduction: LLE reduces the number of features in the data but keeps its important properties. This makes it easier to look at, understand and analyze data with many features.

Disadvantages of LLE

  • Curse of Dimensionality: LLE can experience the "curse of dimensionality" when used with extremely high-dimensional data just like many other dimensionality reduction approaches. The number of neighbors required to capture local interactions rises as dimensionality does, potentially increasing the computational cost of the approach.
  • Memory and computational Requirements: For big datasets creating a weighted adjacency matrix as part of LLE might be memory-intensive. The eigenvalue decomposition stage can also be computationally taxing for big datasets.
  • Outliers and Noisy data: LLE can be sensitive to outliers or noisy data. These unusual points can distort the local patterns and reduce the quality of the results.

Next Article

Similar Reads