LPP, the self-organizing map and incremental grid growing#

Neighborhood-based maps preserve who is next to whom. Locality preserving projections find the linear projection that keeps the kNN graph’s neighbors together. The self-organizing map fits a fixed lattice of prototypes into the data; the U-matrix (mean distance of a node to its lattice neighbors) reveals cluster borders as ridges. Incremental grid growing lets the lattice grow from 2 x 2 nodes where the error is largest and cut lattice links that span gaps in the data.

locality preserving projection, SOM: U-matrix and samples, incremental grid growing (majority class)
import matplotlib.pyplot as plt
import numpy as np

from graphclust import IncrementalGridGrowing, LocalityPreservingProjection, SelfOrganizingMap
from graphclust.datasets import load_wine
from graphclust.plotting import plot_embedding, plot_graph

X, y = load_wine()
X = (X - X.mean(0)) / X.std(0)

# [snippet:som]
lpp = LocalityPreservingProjection(k=10).fit(X)
som = SelfOrganizingMap(grid=(12, 16), topology="hexagonal", n_epochs=60).fit(X)
igg = IncrementalGridGrowing(max_nodes=40, random_state=0).fit(X)
# [/snippet:som]

fig, axes = plt.subplots(1, 3, figsize=(14, 4))
plot_embedding(lpp.embedding_, y, ax=axes[0], title="locality preserving projection")
P = som.grid_positions_
sc = axes[1].scatter(*P.T, c=som.umatrix(), cmap="Greys", s=70, marker="h")
jit = np.random.default_rng(0).normal(0, 0.18, (len(X), 2))
plot_embedding(som.embedding_ + jit, y, ax=axes[1], s=10, title="SOM: U-matrix and samples")
fig.colorbar(sc, ax=axes[1], shrink=0.7)
lab = np.array([np.bincount(y[igg.labels_ == i], minlength=3).argmax() if (igg.labels_ == i).any() else -1 for i in range(len(igg.nodes_))])
plot_graph(igg.grid_positions_, igg.adjacency_, labels=lab, ax=axes[2], node_size=60, title="incremental grid growing (majority class)")
axes[2].set_xticks([])
axes[2].set_yticks([])
plt.tight_layout()

Total running time of the script: (0 minutes 0.264 seconds)

Gallery generated by Sphinx-Gallery