Swiss Roll Reduction with LLE in Scikit Learn (original) (raw)

Last Updated : 7 Apr, 2026

The Swiss roll dataset is a popular non-linear dataset used to test dimensionality reduction algorithms. Even though it appears in 3D, its true structure lies on a 2D surface. Methods like LLE help “unroll” this curved shape and reveal its underlying pattern. It is useful for testing non-linear dimensionality reduction and structure preservation techniques.

swiss_roll_reduction_with_lle

Swiss Roll Reduction with LLE

Understanding LLE Algorithm

Locally Linear Embedding (LLE) is a manifold learning algorithm used for nonlinear dimensionality reduction. It assumes each data point and its neighbors lie on a locally linear patch of the manifold and preserves these local relationships in a lower-dimensional space. It works by:

LLE can be sensitive to the number of neighbors chosen and may not preserve the global shape of the dataset.

**Note: Nonlinear techniques like Locally Linear Embedding (LLE) and t-SNE are better at preserving the curved structure but are more computationally expensive.

Implementation of Swiss Roll Reduction with LLE

We will implement swiss roll reduction using LLE using scikit-learn library.

1. Importing Required Libraries

We begin by importing the Python libraries required for generating data, performing dimensionality reduction and visualization.

import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from sklearn.datasets import make_swiss_roll from sklearn.manifold import LocallyLinearEmbedding from sklearn.decomposition import PCA

`

**2. Generating Swiss Roll Dataset

Next, we create the synthetic 3D dataset that will be used for the experiment.

X, color = make_swiss_roll(n_samples=1000, noise=0.05)

`

**3. Appling Locally Linear Embedding (LLE)

We now perform nonlinear dimensionality reduction using LLE to map the data into 2D.

lle = LocallyLinearEmbedding(n_components=2, n_neighbors=12) X_lle = lle.fit_transform(X) lle_error = lle.reconstruction_error_

`

4. Appling Principal Component Analysis (PCA)

For comparison, we also reduce the data using PCA, a linear dimensionality reduction technique.

pca = PCA(n_components=2) X_pca = pca.fit_transform(X) pca_error = 1 - np.sum(pca.explained_variance_ratio_)

`

5. Plotting Original Swiss Roll in 3D

We then visualize the original dataset to understand its structure before reduction.

fig = plt.figure(figsize=(15, 4)) ax = fig.add_subplot(131, projection='3d') ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=color, cmap=plt.cm.Spectral) ax.set_title('Original Swiss Roll (3D)')

`

**Output:

Swiss-roll

Swiss roll 3D

6. Plotting 2D Output from LLE

Here, we visualize the 2D representation obtained from the LLE algorithm.

plt.subplot(132) plt.scatter(X_lle[:, 0], X_lle[:, 1], c=color, cmap=plt.cm.Spectral) plt.title(f'LLE (2D), Error: {lle_error:.4f}')

`

**Output:

swiss_roll_reduction_with_lle

Swiss roll flattening - LLE

7. Plotting 2D Output from PCA

Finally, we display the 2D output from PCA to see how it handles the same dataset.

plt.subplot(133) plt.scatter(X_pca[:, 0], X_pca[:, 1], c=color, cmap=plt.cm.Spectral) plt.title(f'PCA (2D), Error: {pca_error:.4f}')

`

**Output:

Swiss-roll-Flattening-PCA-

Swiss roll Flattening - PCA

The plots help us compare how well LLE and PCA keep the original shape of the Swiss Roll after reducing it to 2D. The numbers in the plot titles show the error values. LLE and PCA use different error measures, so their values should not be directly compared as each reflects different aspects of dimensionality reduction.

You can download the complete code here.