Swiss Roll Reduction with LLE in Scikit Learn
Last Updated :
18 Sep, 2024
This article discusses the concept of dimensionality reduction, specifically using the Swiss Roll dataset and the Locally Linear Embedding (LLE) algorithm. The article discusses the process involved in performing Swiss Roll reduction with LLE, including the steps of loading and preprocessing the data, fitting the LLE model, and visualizing the reduced data. The article also provides a Python implementation of the process, with a code walkthrough and output examples. The article concludes with observations on the effectiveness of LLE in reducing the dimensionality of the Swiss Roll dataset.
What is Swiss Roll?
The Swiss roll is a toy dataset in scikit learn that is commonly used for testing and demonstrating nonlinear dimensionality reduction algorithms. It consists of a set of points in three dimensions, arranged in a "roll" shape, such that the points on the roll are mapped to a two-dimensional plane in a nonlinear fashion. The points on the Swiss roll are often colored so that the resulting plot of the reduced data can be visualized more easily.
The Swiss roll dataset is often used because it is easy to generate and visualize, and because it exhibits a nonlinear structure that is not captured by linear dimensionality reduction methods such as PCA. It is also a useful benchmark for comparing the performance of different nonlinear dimensionality reduction algorithms.
What is Swiss Roll reduction?
Swiss roll reduction is the process of reducing the dimensionality of the Swiss roll dataset from three dimensions to two or fewer dimensions, using a dimensionality reduction algorithm. The goal of this process is to represent the original data in a lower-dimensional space while preserving as much of the underlying structure of the data as possible.
There are many techniques that can be used for Swiss roll reduction, including linear techniques such as Principal Component Analysis (PCA) and nonlinear techniques such as Locally Linear Embedding (LLE), Isomap, and t-SNE. Each of these techniques has its own strengths and weaknesses and can be more or less effective depending on the characteristics of the data and the goals of the analysis.
In general, linear techniques such as PCA are faster and more computationally efficient, but may not be able to capture complex nonlinear structures in the data. Nonlinear techniques, on the other hand, can capture more complex structures but may be slower and more computationally intensive. Choosing the right technique for Swiss roll reduction will depend on the specific goals of the analysis and the characteristics of the data.
What is Locally Linear Embedding (LLE)?
Locally Linear Embedding (LLE) is a method for nonlinear dimensionality reduction that is based on the idea of modeling the local structure of the data by reconstructing it from a linear combination of its neighbors. The idea behind LLE is to find a low-dimensional representation of the data that preserves the relationships between nearby points while ignoring the relationships between more distant points.
To perform LLE, the algorithm first selects a set of neighbors for each point in the data, based on some measure of similarity such as Euclidean distance. It then fits a linear model to these neighbors, using the original high-dimensional coordinates as the input variables and the low-dimensional coordinates as the output variables. This allows the algorithm to reconstruct each point in the data as a linear combination of its neighbors, using the low-dimensional coordinates as the coefficients. The low-dimensional coordinates are then optimized to minimize the reconstruction error.
LLE has a number of attractive properties, including its ability to handle high-dimensional data and its ability to preserve the local structure of the data. However, it can be sensitive to the choice of the number of neighbors and the dimensionality of the output space, and may not perform well on data with a complex global structure.
The process involved in Swiss Roll reduction with LLE?
Here is a general outline of the process involved in reducing the dimensionality of the Swiss roll dataset using Locally Linear Embedding (LLE):
- Load the Swiss roll dataset: The first step is to load the Swiss roll dataset and store it in a variable X. This can be done using scikit-learn's make_swiss_roll function.
- Fit the LLE model to the data: Next, you can use scikit-learn's LocallyLinearEmbedding class to fit an LLE model to the data. This requires specifying the number of dimensions in the reduced data (n_components) and the number of neighbors to use in the reconstruction process (n_neighbors). You can also specify other optional parameters, such as the distance metric to use for selecting neighbors.
- Transform the data: Once the LLE model has been fit to the data, you can use the transform method to reduce the dimensionality of the data. This will return a new set of coordinates for the data points, with the specified number of dimensions.
- Optionally, compute the reconstruction error of the model using the reconstruction_error_ attribute of the LocallyLinearEmbedding object.
- Visualize the reduced data: Finally, you can visualize the reduced data using a scatter plot or other suitable visualization technique. This can help you understand the structure of the data and assess the effectiveness of the dimensionality reduction process. Plot the original Swiss Roll data in 3D and the transformed data in the lower-dimensional space using Matplotlib.
Here is an example of a comprehensive Python code that demonstrates how to use Locally Linear Embedding (LLE) for Swiss roll reduction in scikit-learn, including pre-processing and comparison with PCA, and visualization steps
Python
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
# Generate the data
X, y = make_swiss_roll(n_samples=2000, noise=0.5)
# Fit the model with LLE
lle = LocallyLinearEmbedding(n_components=2, n_neighbors=10)
X_lle = lle.fit_transform(X)
reconstruction_error_lle = lle.reconstruction_error_
# Fit the model with PCA
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)
reconstruction_error_pca = 1 - sum(pca.explained_variance_ratio_)
In this code, we are using the make_swiss_roll function from scikit-learn to generate a synthetic 3D dataset, known as the Swiss Roll. The Swiss Roll is a toy dataset used for demonstrating nonlinear dimensionality reduction techniques. It consists of a rolled-up 2D plane, with points on the plane randomly distributed and colored.
Python
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
# Generate the data
X, y = make_swiss_roll(n_samples=2000,
noise=0.5)
Next, we apply two different dimensionality reduction techniques to the Swiss Roll dataset: Locally Linear Embedding (LLE) and Principal Component Analysis (PCA), PCA is applied for comparison purposes with LLE. LLE is a nonlinear dimensionality reduction method that seeks to preserve the local structure of the data by reconstructing each point as a linear combination of its neighbors. PCA, on the other hand, is a linear dimensionality reduction method that seeks to find the directions of maximum variance in the data and projects the data onto a new lower-dimensional space along these directions.
To apply LLE, we use the LocallyLinearEmbedding class from scikit-learn. We initialize the class with the number of dimensions we want to reduce the data to (in this case, 2), and the number of neighbors to use for the reconstruction of each point. Then, we fit the model to the data using the fit_transform method, which returns the transformed data in the lower-dimensional space. We also compute the reconstruction error of the LLE model using the reconstruction_error_ attribute of the LocallyLinearEmbedding object.
Python
# Fit the model with LLE
lle = LocallyLinearEmbedding(n_components=2,
n_neighbors=10)
X_lle = lle.fit_transform(X)
reconstruction_error_lle = lle.reconstruction_error_
To apply PCA, we use the PCA class from scikit-learn. We initialize the class with the number of dimensions we want to reduce the data to (in this case, 2) and fit the model to the data using the fit_transform method, which returns the transformed data in the lower-dimensional space. We also compute the reconstruction error of the PCA model by summing up the explained variance ratios of the selected components.
Python
# Fit the model with PCA
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)
reconstruction_error_pca = 1 - sum(pca.explained_variance_ratio_)
Finally, we plot the original Swiss Roll data in 3D, the transformed Swiss Roll data using LLE in 2D, and the transformed Swiss Roll data using PCA in 2D. The plots show the data points colored by their original position on the rolled-up plane, so we can see how well the dimensionality reduction techniques have preserved the structure of the data. The plot titles also display the reconstruction error for each method.
Python
# Plot the original data
fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X[:, 0], X[:, 1], X[:, 2],
c=y, cmap=plt.cm.cool)
plt.title('Original Swiss Roll')
plt.show()
Output:
In the plot of the original Swiss Roll data, you should see a 3D scatterplot with points colored by their original position on the rolled-up plane. The points should form a spiral shape, with the colors changing smoothly along the spiral. This represents the original structure of the data, with points on the plane randomly distributed and colored.
Python
# Plot the transformed data with LLE
fig = plt.figure(figsize=(6, 6))
plt.scatter(X_lle[:, 0], X_lle[:, 1], c=y, cmap=plt.cm.cool)
plt.title(
f'Transformed Swiss Roll with LLE\
(reconstruction error = {reconstruction_error_lle:.2f})')
plt.show()
Output:
In the plot of the transformed Swiss Roll data using LLE, you should see a 2D scatterplot with points colored by their original position on the rolled-up plane.Â
Python
# Plot the transformed data with PCA
fig = plt.figure(figsize=(6, 6))
plt.scatter(X_pca[:, 0], X_pca[:, 1],
c=y, cmap=plt.cm.cool)
plt.title(
f'Transformed Swiss Roll with PCA \
(reconstruction error = {reconstruction_error_pca:.2f})')
plt.show()
Output:
In the plot of the transformed Swiss Roll data using PCA, you should see a 2D scatterplot with points colored by their original position on the rolled-up plane. The points should not necessarily form a spiral shape, as PCA is a linear dimensionality reduction method and may not be able to capture the nonlinear structure of the data. However, the points should still be somewhat ordered by color, indicating that PCA has been able to preserve some of the structure of the data by finding the directions of maximum variance.
Overall, the output plots allow us to compare the performance of LLE and PCA in preserving the structure of the Swiss Roll data after dimensionality reduction. The reconstruction error for each method, displayed in the plot titles, provides a quantitative measure of this performance. A lower reconstruction error indicates better preservation of the original structure of the data.
Similar Reads
Machine Learning Algorithms
Machine learning algorithms are essentially sets of instructions that allow computers to learn from data, make predictions, and improve their performance over time without being explicitly programmed. Machine learning algorithms are broadly categorized into three types: Supervised Learning: Algorith
8 min read
Top 15 Machine Learning Algorithms Every Data Scientist Should Know in 2025
Machine Learning (ML) Algorithms are the backbone of everything from Netflix recommendations to fraud detection in financial institutions. These algorithms form the core of intelligent systems, empowering organizations to analyze patterns, predict outcomes, and automate decision-making processes. Wi
14 min read
Linear Model Regression
Ordinary Least Squares (OLS) using statsmodels
Ordinary Least Squares (OLS) is a widely used statistical method for estimating the parameters of a linear regression model. It minimizes the sum of squared residuals between observed and predicted values. In this article we will learn how to implement Ordinary Least Squares (OLS) regression using P
3 min read
Linear Regression (Python Implementation)
Linear regression is a statistical method that is used to predict a continuous dependent variable i.e target variable based on one or more independent variables. This technique assumes a linear relationship between the dependent and independent variables which means the dependent variable changes pr
14 min read
Multiple Linear Regression using Python - ML
Linear regression is a statistical method used for predictive analysis. It models the relationship between a dependent variable and a single independent variable by fitting a linear equation to the data. Multiple Linear Regression extends this concept by modelling the relationship between a dependen
4 min read
Polynomial Regression ( From Scratch using Python )
Prerequisites Linear RegressionGradient DescentIntroductionLinear Regression finds the correlation between the dependent variable ( or target variable ) and independent variables ( or features ). In short, it is a linear model to fit the data linearly. But it fails to fit and catch the pattern in no
5 min read
Bayesian Linear Regression
Linear regression is based on the assumption that the underlying data is normally distributed and that all relevant predictor variables have a linear relationship with the outcome. But In the real world, this is not always possible, it will follows these assumptions, Bayesian regression could be the
10 min read
How to Perform Quantile Regression in Python
In this article, we are going to see how to perform quantile regression in Python. Linear regression is defined as the statistical method that constructs a relationship between a dependent variable and an independent variable as per the given set of variables. While performing linear regression we a
4 min read
Isotonic Regression in Scikit Learn
Isotonic regression is a regression technique in which the predictor variable is monotonically related to the target variable. This means that as the value of the predictor variable increases, the value of the target variable either increases or decreases in a consistent, non-oscillating manner. Mat
6 min read
Stepwise Regression in Python
Stepwise regression is a method of fitting a regression model by iteratively adding or removing variables. It is used to build a model that is accurate and parsimonious, meaning that it has the smallest number of variables that can explain the data. There are two main types of stepwise regression: F
6 min read
Least Angle Regression (LARS)
Regression is a supervised machine learning task that can predict continuous values (real numbers), as compared to classification, that can predict categorical or discrete values. Before we begin, if you are a beginner, I highly recommend this article. Least Angle Regression (LARS) is an algorithm u
3 min read
Linear Model Classification
K-Nearest Neighbors (KNN)
ML - Stochastic Gradient Descent (SGD)
Stochastic Gradient Descent (SGD) is an optimization algorithm in machine learning, particularly when dealing with large datasets. It is a variant of the traditional gradient descent algorithm but offers several advantages in terms of efficiency and scalability, making it the go-to method for many d
8 min read