Note
Go to the end to download the full example code or to run this example in your browser via Binder.
OVI-NG, GNLP-NG and TRNMap#
All three map the nodes of a neural-gas-type network:
OVI-NG adapts node positions online together with the codebook, matching map distances to Euclidean codebook distances; the S curve stays folded;
GNLP-NG uses geodesic distances on the node graph and unfolds it;
TRNMap builds a topology representing network, connects it, computes geodesic distances and maps them by metric MDS (DP-TRNMap) or non-metric MDS (NP-TRNMap) in one shot, without online iterations.
The edges of the learned graphs are drawn in the maps; the nodes are colored by the position along the S of the samples they represent.

import matplotlib.pyplot as plt
import numpy as np
from graphclust import GeodesicNLPNG, OnlineVisualizationNG, TRNMap
from graphclust.datasets import make_s_curve
from graphclust.plotting import plot_graph
X, t = make_s_curve(2000, random_state=0)
n = 120
# [snippet:trnmap]
ovi = OnlineVisualizationNG(n_nodes=n, random_state=0).fit(X)
gnlp = GeodesicNLPNG(n_nodes=n, random_state=0).fit(X)
dp = TRNMap(n_nodes=n, mapping="metric", random_state=0).fit(X)
np_ = TRNMap(n_nodes=n, mapping="nonmetric", random_state=0).fit(X)
# [/snippet:trnmap]
fig, axes = plt.subplots(1, 4, figsize=(14, 3.6))
for ax, m, name in [(axes[0], ovi, "OVI-NG"), (axes[1], gnlp, "GNLP-NG"), (axes[2], dp, "DP-TRNMap"), (axes[3], np_, "NP-TRNMap")]:
owner = np.argmin(((X[:, None, :] - m.nodes_[None]) ** 2).sum(-1), axis=1)
t_node = np.array([t[owner == i].mean() if (owner == i).any() else np.nan for i in range(len(m.nodes_))])
plot_graph(m.embedding_, m.adjacency_, ax=ax, node_size=0, lw=0.5, title=name)
ax.scatter(*m.embedding_.T, c=t_node, cmap="viridis", s=16, zorder=3)
ax.set_xticks([])
ax.set_yticks([])
plt.tight_layout()
Total running time of the script: (0 minutes 2.791 seconds)