Note
Go to the end to download the full example code or to run this example in your browser via Binder.
Laplacian eigenmaps and diffusion maps#
Spectral embeddings use the eigenvectors of a graph operator: Laplacian eigenmaps take the smoothest non-constant functions on the kNN graph, diffusion maps the leading eigenvectors of a random walk on the data, scaled by their eigenvalues so that map distances approximate diffusion distances. Both are global, deterministic and fast, and both serve as initialization of the neighbor-embedding methods (UMAP starts from a spectral layout).

import matplotlib.pyplot as plt
from graphclust import DiffusionMap, LaplacianEigenmap
from graphclust.datasets import make_s_curve, make_swiss_roll
from graphclust.plotting import plot_embedding
fig, axes = plt.subplots(1, 4, figsize=(14, 3.5))
for i, (X, t, name) in enumerate([(*make_s_curve(1500, random_state=0), "S curve"), (*make_swiss_roll(1500, random_state=0), "swiss roll")]):
# [snippet:spectral]
le = LaplacianEigenmap(k=10).fit(X)
dm = DiffusionMap(k=10, alpha=1.0).fit(X)
# [/snippet:spectral]
plot_embedding(le.embedding_, t, ax=axes[2 * i], s=5, title=f"Laplacian eigenmap, {name}")
plot_embedding(dm.embedding_, t, ax=axes[2 * i + 1], s=5, title=f"diffusion map, {name}")
plt.tight_layout()
Total running time of the script: (0 minutes 0.622 seconds)