SOUMAP + MPEC Tutorial¶
This notebook demonstrates the full SOUMAP → MPEC workflow on a synthetic dataset designed to make the results easy to interpret.
Stages covered:
- Background: what SOUMAP does and why
- Generate data
- Configure and train SOUMAP
- Inspect the SOUMAP embedding (before and after training)
- Run MPEC clustering on the SOUMAP outputs
- Interpret the MPEC diagnostic plots
Data: sklearn.make_blobs, N=5000, D=10, K=5 clusters, cluster_std=1.5
(moderate overlap — not trivially separable).
1. Background¶
Self-Organizing Maps¶
A Self-Organizing Map (SOM) is an unsupervised neural network that learns a compressed, topology-preserving representation of high-dimensional data. Training places a fixed set of M prototype vectors W into the data space such that nearby prototypes in the output space respond to nearby regions of the input space. At each training step, the closest prototype to a data point (the best-matching unit, or BMU) and its output-space neighbors are nudged toward that point. The neighborhood bandwidth shrinks across training, so the map progressively refines its representation of the data.
In a classical SOM the output-space topology is a fixed rectangular or hexagonal
grid chosen before training begins — it has nothing to do with the data itself.
GTSOM (gtsom) removes this constraint:
prototypes are seeded by a low-dimensional projection of the data (e.g. PCA), and
the adjacency graph is constructed from those projected positions (Gabriel graph
or Delaunay triangulation). The topology therefore reflects the data geometry
from the very first training step.
UMAP¶
UMAP (Uniform Manifold Approximation and Projection) is a dimensionality
reduction algorithm that models the data manifold and finds a low-dimensional
layout that preserves its topological structure. Unlike PCA, UMAP captures
non-linear relationships and tends to produce tight, well-separated clusters when
they exist in the data. The key user-controlled parameters are n_neighbors
(controls how local vs. global the view is) and min_dist (controls how tightly
points are packed in the embedding).
SOUMAP: the hybrid¶
SOUMAP alternates between a SOM step and a UMAP step, coupling the two algorithms so that each informs the other:
- SOM step — prototype vectors W are updated by batch SOM learning. The output-space topology used to define neighborhoods is derived from a Gabriel graph (or Delaunay triangulation) of the current embedding coordinates Y.
- UMAP step — a UMAP similarity graph is built from the co-adjacency matrix (CADJ) produced by VQ recall. The UMAP layout optimizer updates Y, warm-started from its current state. The new Y becomes the SOM's output topology for the next SOM step.
Two key differences from the standard versions:
- Unlike a classical SOM, the output lattice is not fixed — it evolves with the data structure.
- Unlike standard UMAP, the similarity graph is not user-prescribed — it is
inferred from the learned VQ topology, so there is no
n_neighborsparameter to tune.
The result is a set of M prototype vectors that tile the data density, together with a 2-D embedding of those prototypes that faithfully reflects their high-dimensional geometry.
MPEC: from embedding to clustering¶
Once SOUMAP has been trained it yields three complementary views of the prototype space:
| View | What it captures |
|---|---|
| CADJ | Density + topology — a count matrix recording how many data points fall in the second-order Voronoi cells shared by each prototype pair, encoding both local density and high-D topology |
| W | High-D geometry — Euclidean proximity in feature space |
| Y | Low-D geometry — Euclidean proximity in the embedding |
MPEC (Multiview Prototype Embedding & Clustering) fuses these three views into a single weighted graph using self-tuning kernels, then partitions the graph with the Walktrap community-detection algorithm (Pons & Latapy, 2005). The number of random-walk steps is found automatically by analyzing the tradeoff between Markov-chain mixing and cluster cohesion across steps.
2. Imports¶
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from soumap import SOUMAP, SOMParams, UMAPParams, CtrlParams, MPEClustering
3. Generate data¶
We use sklearn.make_blobs to generate 5 Gaussian clusters in 10 dimensions
with 5,000 total samples. cluster_std=1.5 gives moderate overlap — the clusters
are not trivially separable, making this a good stress-test for the algorithm.
N, D, K, M = 5000, 10, 5, 100
X, y_true = make_blobs(
n_samples=N,
n_features=D,
centers=K,
cluster_std=1.5,
random_state=42,
)
print(f"X: {X.shape}")
print(f"Ground-truth classes: {np.unique(y_true)}")
X: (5000, 10) Ground-truth classes: [0 1 2 3 4]
4. Configure and train SOUMAP¶
SOUMAP is configured via three parameter dataclasses:
SOMParams— controls the SOM learning dynamics (neighborhood bandwidth schedule, output-space topology type, prototype topology).UMAPParams— controls the UMAP layout optimizer (update frequency, number of epochs,min_distandspreadschedules, learning rate schedule).CtrlParams— controls the overall training loop (total epochs, initialization strategy, random seed, verbosity).
After construction, tune_embed_scale() calibrates rho_0, min_dist_0, and
spread_0 from the null geometry of the data — this step sets the initial
neighborhood and embedding scales to sensible values without manual tuning.
build() initializes the prototype vectors and embedding coordinates; fit()
runs the alternating SOM/UMAP training loop.
Note that we use coord_init='random' here so that the prototypes start in a
completely unorganized state. This makes the learning that SOUMAP performs more
apparent when comparing the initial and final embeddings.
som_params = SOMParams(
rho_0=2.0, # overwritten by tune_embed_scale()
rho_f=0.5,
nbr_topo_alpha_0=0.5,
nbr_topo_alpha_f=1.0,
coord_topo='gabriel',
proto_topo='STK_CADJ',
compute_dr_metrics=False,
)
umap_params = UMAPParams(
update_freq=5,
n_epochs=100,
min_dist_0=0.1, # overwritten by tune_embed_scale()
min_dist_f=0.1,
spread_0=2.0, # overwritten by tune_embed_scale()
spread_f=2.0,
lrate_0=1.0,
lrate_f=0.5,
negative_sample_rate=5,
gamma=1.0,
use_standard_umap=False,
)
ctrl_params = CtrlParams(
total_epochs=100,
min_nhbs=3,
fill_val=1,
coord_init='random', # random init makes the learned organization more apparent
embedding_range=(-3.0, 3.0),
n_jobs=1, # sequential = reproducible
random_state=42,
verbose=True,
plot_every=0,
)
model = SOUMAP(
M=M,
som_params=som_params,
umap_params=umap_params,
ctrl_params=ctrl_params,
)
print("Tuning embedding scale parameters...")
model.tune_embed_scale(rho0_scale=0.75, min_dist0_scale=1.0, min_distf_scale=0.1)
print(f" rho_0 = {model.som_params.rho_0:.4f}")
print(f" min_dist_0 = {model.umap_params.min_dist_0:.4f}")
print(f" spread_0 = {model.umap_params.spread_0:.4f}")
print("\nBuilding SOUMAP architecture...")
model.build(X, labels=y_true)
Tuning embedding scale parameters... tune_embed_scale: null_max_geo_dist=8.0000, null_mean_geo_dist=4.2364, null_mean_edge_len=0.7208 | rho_0=6.0000, min_dist_0=0.7208, spread_0=2.0000 rho_0 = 6.0000 min_dist_0 = 0.7208 spread_0 = 2.0000 Building SOUMAP architecture... Running FAISS k-means with 100 clusters (niter=20, nredo=1)... K-means complete. Final MQE: 13.534280 Fitting complete using kmeans method.
SOUMAP(M=100, total_epochs=100, age=0)
Initialization (age = 0)¶
Before training begins, prototype embedding coordinates are assigned randomly. The prototypes are scattered with no organization — cluster labels are mixed throughout the embedding, and the Gabriel graph edges cross freely between groups. There is no correspondence yet between position in the 2-D output space and identity in the 10-D feature space.
g_init = model.gtsom.plot(
color_by='labels',
title='SOUMAP — Blobs',
subtitle=f'Initialization (age=0), N={N}, D={D}, K={K}, M={M}',
)
g_init.draw()
print("Fitting SOUMAP (100 epochs)...")
model.fit(X, labels=y_true)
print(f"Done. age={model.age}")
Fitting SOUMAP (100 epochs)...
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
1 6.0000 0.5000 12.2412 0.9448
2 5.8527 0.5035 10.6174 1.0000
3 5.7091 0.5070 9.8206 0.8792
4 5.5690 0.5105 8.9584 0.7988
5 5.4323 0.5141 9.2314 0.6322
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=4, mean_nnz=3.0, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=6, mean_nnz=3.7, val_range=[0.006, 1.000]
UMAP update age=5 rho=5.2990 alpha=0.5176 lrate=0.9659 min_dist=0.6424
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
6 5.2990 0.5176 6.3456 0.9330
7 5.1689 0.5212 5.7385 0.9632
8 5.0421 0.5249 5.8442 0.9550
9 4.9183 0.5285 5.5923 0.8762
10 4.7976 0.5322 5.6757 0.5324
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=5, mean_nnz=3.0, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=6, mean_nnz=3.5, val_range=[0.027, 1.000]
UMAP update age=10 rho=4.6799 alpha=0.5359 lrate=0.9330 min_dist=0.5726
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
11 4.6799 0.5359 7.9589 0.9240
12 4.5650 0.5396 7.2679 0.7688
13 4.4530 0.5434 7.1538 0.7886
14 4.3437 0.5471 7.1362 0.9906
15 4.2371 0.5510 6.7187 0.9978
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=3, mean_nnz=3.0, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=8, mean_nnz=4.3, val_range=[0.001, 1.000]
UMAP update age=15 rho=4.1331 alpha=0.5548 lrate=0.9013 min_dist=0.5103
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
16 4.1331 0.5548 8.2157 0.7898
17 4.0316 0.5586 7.2536 0.9322
18 3.9327 0.5625 7.2325 0.8110
19 3.8362 0.5664 6.6611 0.3872
20 3.7420 0.5704 6.0629 0.9782
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=4, mean_nnz=3.0, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=7, mean_nnz=4.0, val_range=[0.001, 1.000]
UMAP update age=20 rho=3.6502 alpha=0.5743 lrate=0.8706 min_dist=0.4548
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
21 3.6502 0.5743 9.0065 0.7834
22 3.5606 0.5783 7.5945 0.9810
23 3.4732 0.5824 6.3290 0.9066
24 3.3880 0.5864 5.5870 0.9418
25 3.3048 0.5905 5.4408 0.7728
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=5, mean_nnz=3.0, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=6, mean_nnz=4.0, val_range=[0.035, 1.000]
UMAP update age=25 rho=3.2237 alpha=0.5946 lrate=0.8409 min_dist=0.4054
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
26 3.2237 0.5946 8.4686 0.8598
27 3.1446 0.5987 7.0334 0.8042
28 3.0674 0.6029 6.1082 0.7220
29 2.9921 0.6071 5.3752 0.7178
30 2.9187 0.6113 5.2156 0.5820
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=6, mean_nnz=3.2, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=7, mean_nnz=4.0, val_range=[0.065, 1.000]
UMAP update age=30 rho=2.8471 alpha=0.6156 lrate=0.8123 min_dist=0.3613
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
31 2.8471 0.6156 6.6309 0.7670
32 2.7772 0.6199 6.1178 0.6836
33 2.7090 0.6242 5.9489 0.6188
34 2.6425 0.6285 5.8711 0.5890
35 2.5777 0.6329 5.8347 0.5718
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=9, mean_nnz=3.9, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=10, mean_nnz=4.9, val_range=[0.013, 1.000]
UMAP update age=35 rho=2.5144 alpha=0.6373 lrate=0.7846 min_dist=0.3220
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
36 2.5144 0.6373 5.0438 0.7598
37 2.4527 0.6417 4.5987 0.6232
38 2.3925 0.6462 4.5082 0.4866
39 2.3338 0.6507 4.4564 0.4082
40 2.2765 0.6552 4.4416 0.3304
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=8, mean_nnz=3.8, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=10, mean_nnz=4.8, val_range=[0.028, 1.000]
UMAP update age=40 rho=2.2206 alpha=0.6598 lrate=0.7579 min_dist=0.2870
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
41 2.2206 0.6598 4.8007 0.6000
42 2.1661 0.6643 4.5776 0.3734
43 2.1130 0.6690 4.5302 0.3404
44 2.0611 0.6736 4.4855 0.3536
45 2.0105 0.6783 4.4550 0.3432
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=10, mean_nnz=4.1, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=12, mean_nnz=5.0, val_range=[0.014, 1.000]
UMAP update age=45 rho=1.9612 alpha=0.6830 lrate=0.7320 min_dist=0.2558
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
46 1.9612 0.6830 4.4422 0.5226
47 1.9131 0.6878 4.4167 0.2432
48 1.8661 0.6926 4.4038 0.1550
49 1.8203 0.6974 4.3918 0.0946
50 1.7756 0.7022 4.3828 0.0730
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=11, mean_nnz=4.2, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=12, mean_nnz=5.1, val_range=[0.021, 1.000]
UMAP update age=50 rho=1.7321 alpha=0.7071 lrate=0.7071 min_dist=0.2279
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
51 1.7321 0.7071 4.6600 0.8226
52 1.6895 0.7120 4.4638 0.4296
53 1.6481 0.7170 4.4352 0.2088
54 1.6076 0.7220 4.4219 0.1438
55 1.5682 0.7270 4.4059 0.1148
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=13, mean_nnz=4.5, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=14, mean_nnz=5.9, val_range=[0.022, 1.000]
UMAP update age=55 rho=1.5297 alpha=0.7320 lrate=0.6830 min_dist=0.2032
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
56 1.5297 0.7320 4.6144 0.6378
57 1.4921 0.7371 4.4049 0.4442
58 1.4555 0.7423 4.3616 0.2256
59 1.4198 0.7474 4.3393 0.1710
60 1.3850 0.7526 4.3204 0.1504
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=13, mean_nnz=4.9, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=13, mean_nnz=6.0, val_range=[0.070, 1.000]
UMAP update age=60 rho=1.3510 alpha=0.7579 lrate=0.6598 min_dist=0.1811
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
61 1.3510 0.7579 4.3647 0.4798
62 1.3178 0.7631 4.3137 0.2658
63 1.2855 0.7684 4.2776 0.1992
64 1.2539 0.7738 4.2535 0.1312
65 1.2231 0.7792 4.2365 0.1024
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=12, mean_nnz=5.6, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=13, mean_nnz=7.4, val_range=[0.026, 1.000]
UMAP update age=65 rho=1.1931 alpha=0.7846 lrate=0.6373 min_dist=0.1614
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
66 1.1931 0.7846 4.3619 0.5962
67 1.1638 0.7900 4.3020 0.3026
68 1.1353 0.7955 4.2695 0.1388
69 1.1074 0.8011 4.2518 0.0942
70 1.0802 0.8066 4.2300 0.1302
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=14, mean_nnz=5.9, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=15, mean_nnz=7.6, val_range=[0.057, 1.000]
UMAP update age=70 rho=1.0537 alpha=0.8123 lrate=0.6156 min_dist=0.1438
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
71 1.0537 0.8123 4.3971 0.5902
72 1.0279 0.8179 4.2560 0.3228
73 1.0026 0.8236 4.2209 0.1638
74 0.9780 0.8293 4.2039 0.1054
75 0.9540 0.8351 4.1878 0.0784
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=12, mean_nnz=6.5, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=14, mean_nnz=8.5, val_range=[0.090, 1.000]
UMAP update age=75 rho=0.9306 alpha=0.8409 lrate=0.5946 min_dist=0.1282
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
76 0.9306 0.8409 4.2707 0.4716
77 0.9078 0.8467 4.2014 0.2360
78 0.8855 0.8526 4.1670 0.1436
79 0.8638 0.8586 4.1344 0.1330
80 0.8426 0.8645 4.1107 0.0946
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=14, mean_nnz=7.8, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=18, mean_nnz=9.7, val_range=[0.088, 1.000]
UMAP update age=80 rho=0.8219 alpha=0.8706 lrate=0.5743 min_dist=0.1142
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
81 0.8219 0.8706 4.2218 0.4718
82 0.8017 0.8766 4.1472 0.2492
83 0.7820 0.8827 4.1067 0.1458
84 0.7628 0.8888 4.0827 0.1092
85 0.7441 0.8950 4.0641 0.0854
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=17, mean_nnz=8.9, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=18, mean_nnz=10.9, val_range=[0.132, 1.000]
UMAP update age=85 rho=0.7259 alpha=0.9013 lrate=0.5548 min_dist=0.1018
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
86 0.7259 0.9013 4.1614 0.4046
87 0.7080 0.9075 4.0983 0.1992
88 0.6907 0.9138 4.0629 0.1292
89 0.6737 0.9202 4.0372 0.0880
90 0.6572 0.9266 4.0179 0.0632
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=16, mean_nnz=9.4, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=19, mean_nnz=11.7, val_range=[0.103, 1.000]
UMAP update age=90 rho=0.6410 alpha=0.9330 lrate=0.5359 min_dist=0.0907
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
91 0.6410 0.9330 4.0504 0.2930
92 0.6253 0.9395 4.0039 0.1492
93 0.6100 0.9461 3.9735 0.1024
94 0.5950 0.9526 3.9497 0.0732
95 0.5804 0.9593 3.9314 0.0614
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=20, mean_nnz=10.2, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=20, mean_nnz=12.2, val_range=[0.135, 1.000]
UMAP update age=95 rho=0.5661 alpha=0.9659 lrate=0.5176 min_dist=0.0809
Epoch rho alpha MQE delBMU
----- ------ ------ -------- -------
96 0.5661 0.9659 3.9795 0.2670
97 0.5523 0.9727 3.9449 0.1332
98 0.5387 0.9794 3.9197 0.0904
99 0.5255 0.9862 3.8997 0.0584
100 0.5126 0.9931 3.8824 0.0436
----- ------ ------ -------- -------
PCADJ: min_nnz=3, max_nnz=17, mean_nnz=11.2, empty_rows=0, below_min_nhbs=0
FSIM: min_nnz=3, max_nnz=20, mean_nnz=13.4, val_range=[0.155, 1.000]
UMAP update age=100 rho=0.5000 alpha=1.0000 lrate=0.5000 min_dist=0.0721
Done. age=100
After training (age = 100)¶
After 100 training epochs the five clusters are tightly packed and clearly separated in the embedding. The inter-cluster edges have disappeared: the Gabriel graph now connects only prototypes within the same cluster. Each cluster occupies a compact, internally connected region of the 2-D output space, and the relative positions of the clusters faithfully reflect their separation in the original 10-D feature space. The contrast with the random initialization above illustrates how much organization SOUMAP learns jointly across the SOM and UMAP steps.
g_final = model.gtsom.plot(
color_by='labels',
title='SOUMAP — Blobs',
subtitle=f'N={N}, D={D}, K={K}, M={M}, age={model.age}',
)
g_final.draw()
5. Save and reload SOUMAP¶
Models can be saved to HDF5 and reloaded, preserving all prototype vectors, embedding coordinates, and learned topology.
model.save('blobs_soumap.h5')
model = SOUMAP.load('blobs_soumap.h5')
print(f"Reloaded: age={model.age}, W={model.W.shape}, coords={model.coords.shape}")
Reloaded: age=100, W=(100, 10), coords=(100, 2)
6. Run MPEC clustering¶
MPEC takes three inputs directly from the trained SOUMAP:
- W — the prototype vectors (high-D view)
- Y (
model.coords) — the 2-D embedding coordinates (low-D view) - CADJ — the Cumulative Adjacency Matrix (Tasdemir & Merenyi, 2009). CADJ[i,j] counts the number of data points that fall in the second-order Voronoi cell shared by prototypes i and j — i.e. points for which i and j are the first and second BMU, or vice versa. Because its entries are counts rather than binary flags, CADJ encodes both high-D topology (which prototypes are adjacent in data space) and an unnormalized view of local density (how many data points lie in the shared region between them).
These are fused into a single weighted graph via self-tuning kernels, then Walktrap community detection partitions the graph into clusters.
With walktrap_n_steps="auto", MPEC analyzes the mixing/cohesion tradeoff
across walk lengths to select the optimal step count automatically.
W = model.W
Y = model.coords
CADJ = model.gtsom.recaller.CADJ
CADJ_nhbs = model.gtsom.recaller.CADJ_nhbs
mpec = MPEClustering(
walktrap_n_steps="auto",
kernel_w_cadj=1.0,
kernel_w_high_d=1.0,
kernel_w_low_d=1.0,
kernel_power=2.0,
verbose=True,
)
mpec.fit(W=W, Y=Y, CADJ=CADJ, CADJ_nhbs=CADJ_nhbs)
n_clusters = len(np.unique(mpec.active_labels_))
print(f"\nGround-truth K = {K}")
print(f"MPEC found = {n_clusters} clusters")
print(f"Walktrap steps used: {mpec.n_steps_}")
[MPEClustering] Starting fit: M=100 prototypes, W.shape=(100, 10), Y.shape=(100, 2) [MPEClustering] Found 3/100 empty prototypes (3.0%). [MPEClustering] Clustering 97 active prototypes. [MPEClustering] 2 prototype(s) have fewer than 3 CADJ neighbours after subsetting. CADJ_self_tuning_kernel will pad internally for the Euclidean views; kernel_cadj_ is left unpadded so genuine low-density rows remain visible in the fused graph. [MPEClustering] Building high-D Euclidean kernel view... [MPEClustering] Building low-D Euclidean kernel view... [MPEClustering] Fusing views into weighted graph... [MPEClustering] Fused graph: 97 vertices, 1014 edges (density=0.216). [MPEClustering] Finding optimal Walktrap step count... [MPEClustering] Step analysis: 4 communicating class(es), sizes: [33, 30, 19, 15] [MPEClustering] Component 0 (size=33): CC(1)=0.9925, HD(1)=0.6336 [MPEClustering] Component 0 (size=33): crossing at step=3 (cc_norm=1.0000, hd_norm=0.8668) [MPEClustering] Component 1 (size=30): CC(1)=0.9937, HD(1)=0.4890 [MPEClustering] Component 1 (size=30): crossing at step=2 (cc_norm=1.0000, hd_norm=0.6287) [MPEClustering] Component 2 (size=19): CC(1)=1.0000, HD(1)=0.4278 [MPEClustering] [WARNING] Component 2 (size=19): no crossing found within max_steps=15. Using best_step=1. Consider increasing walktrap_max_steps. [MPEClustering] Component 2 (size=19): crossing at step=1 (cc_norm=0.0000, hd_norm=1.0000) [MPEClustering] Component 3 (size=15): CC(1)=1.0000, HD(1)=0.3705 [MPEClustering] [WARNING] Component 3 (size=15): no crossing found within max_steps=15. Using best_step=1. Consider increasing walktrap_max_steps. [MPEClustering] Component 3 (size=15): crossing at step=1 (cc_norm=0.0000, hd_norm=1.0000) [MPEClustering] Selected n_steps=2. [MPEClustering] Running Walktrap (steps=2)... [MPEClustering] Walktrap complete: 5 clusters found. [MPEClustering] Done. 5 clusters over 97 active prototypes (3 empty, labelled -1). Ground-truth K = 5 MPEC found = 5 clusters Walktrap steps used: 2
7. MPEC diagnostic plots¶
MPEC provides six diagnostic plots, each illuminating a different aspect of the clustering process. They are generated and described individually below.
7.1 Walktrap step tradeoff¶
Walktrap partitions a graph by simulating short random walks and inferring that nodes frequently visited together belong to the same community. The length of those walks — the number of steps t — is a critical choice. Pons & Latapy offered intuitive guidance: we want to iterate the Markov chain long enough that intra-cluster structure becomes apparent, but not so long that the chain approaches its stationary distribution, at which point all memory of where the walk started is lost and community structure disappears.
MPEC operationalizes this tradeoff by tracking two normalized quantities as t increases:
- Transience (TR) — the normalized Hellinger distance of the transition matrix rows from the stationary distribution. TR = 1 at step 1 (the chain is fully transient) and decreases monotonically toward 0 as the walk mixes. High TR means the walker still "remembers" where it started — community information is preserved.
- Cluster Cohesion (CC) — the normalized gain in the Barrat weighted clustering coefficient since step 1. CC starts at 0 and rises as the multi-step transition probabilities concentrate within clusters, making intra-cluster triangles more prominent in the walk graph.
The optimal step is the first crossing point — the first t where CC ≥ TR.
At this point, the cohesion gain has caught up with the transience loss: the walk
has run long enough to reveal cluster structure, but not so long that the
stationary distribution has erased it. The dashed vertical line marks the
selected step.
On the blobs data, step 1 is selected for all components, indicating that the fused graph already has such strong community structure that even a single-step walk is sufficient for Walktrap to find the correct partition.
panel = mpec.plot_diagnostics(which=["step_tradeoff"], figsize=(6, 5))
panel["step_tradeoff"].draw()
[MPEClustering] Rendering: step_tradeoff...
7.2 Kernel edge weight distributions¶
Distributions of edge weights in each of the three kernel views (CADJ, High-D, Low-D) after self-tuning normalization. On the blobs data, all three views produce bimodal distributions with a peak near 0 (weak inter-cluster edges) and a peak near 1 (strong intra-cluster edges), confirming that the cluster structure is cleanly captured by every view.
panel = mpec.plot_diagnostics(which=["edge_distributions"], figsize=(6, 5))
panel["edge_distributions"].draw()
[MPEClustering] Rendering: edge_distributions...
7.3 Modularity curve¶
Walktrap modularity as a function of the number of clusters k, across the full dendrogram. The optimal cut is the k that maximizes modularity within the valid range shown in the subtitle. On the blobs data, modularity peaks sharply at k = 5, exactly matching the ground-truth number of clusters.
panel = mpec.plot_diagnostics(which=["modularity"], figsize=(6, 5))
panel["modularity"].draw()
[MPEClustering] Rendering: modularity...
7.4 Cluster sizes¶
Number of active prototypes assigned to each cluster; the dashed line marks the mean. On the blobs data, clusters are roughly balanced (~19–20 prototypes each), reflecting the balanced ground-truth class sizes.
panel = mpec.plot_diagnostics(which=["cluster_sizes"], figsize=(6, 5))
panel["cluster_sizes"].draw()
[MPEClustering] Rendering: cluster_sizes...
7.5 Embedding with cluster colors¶
The SOUMAP embedding colored by MPEC cluster assignment, with fused graph edges overlaid. Each of the five clusters occupies a distinct, compact region of the 2-D space. No inter-cluster edges appear, confirming that the Walktrap cut correctly partitions the five groups.
panel = mpec.plot_diagnostics(
embed_coords=Y,
show_graph=True,
which=["embedding"],
point_size=3.0,
figsize=(6, 5),
)
panel["embedding"].draw()
[MPEClustering] Rendering: embedding...
7.6 Kernel view agreement¶
A 3×3 heatmap of Frobenius cosine similarity between each pair of kernel views. High agreement (near 1.0) means two views encode similar structure; lower values mean they contribute complementary information. On the blobs data, the High-D and Low-D views agree most strongly (0.85) — expected, since both capture Euclidean proximity and the SOUMAP embedding faithfully reflects feature-space geometry. The CADJ view agrees moderately with each (0.56–0.62), reflecting that its density-weighted topology encodes a subtly different aspect of the prototype relationships than pure geometric distance.
panel = mpec.plot_diagnostics(which=["view_agreement"], figsize=(6, 5))
panel["view_agreement"].draw()
[MPEClustering] Rendering: view_agreement...
8. Summary¶
In this tutorial we trained SOUMAP on a synthetic 10-D dataset with 5 moderately overlapping Gaussian clusters and then applied MPEC to recover those clusters from the learned prototype representation.
Key takeaways:
- SOUMAP jointly learns M prototype vectors and their 2-D embedding, with the embedding topology evolving during training rather than being fixed in advance. Starting from a random initialization makes the degree of learned organization clearly visible.
- After training, the embedding clearly separates the five clusters even though they overlap in the original 10-D space.
- MPEC fuses three complementary views of the prototype space — CADJ (density + topology), high-D geometry, and low-D geometry — and recovers the correct number of clusters without requiring k to be specified by the user.
- The six diagnostic plots provide insight into the clustering process: the step-tradeoff plot explains the walk-length selection, the modularity curve shows the evidence for the chosen k, and the embedding plot gives an interpretable view of the result.
Next steps:
- Apply the pipeline to real data (e.g. MNIST handwritten digits — see
test_mnist.pyin the repository). - Experiment with
kernel_powerand the per-view weights to emphasize different aspects of the prototype structure. - Use
model.transform(X_new)to map new data points to the learned embedding.