U-matrix
data visualization
self-organizing maps
machine learning
neural networks

How do I make a U-matrix?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

A U-matrix, short for Unified Distance Matrix, is a standard way to visualize the structure of a trained self-organizing map. Instead of plotting the raw weight vectors directly, it shows how different neighboring neurons are from one another. That makes it useful for spotting cluster boundaries, dense regions, and transitions across the map.

Start From the Trained SOM Weights

To build a U-matrix, you need the codebook vectors, sometimes just called the SOM weights. If your map has rows by cols neurons and each neuron stores a feature vector of length d, the weights are commonly represented as an array of shape (rows, cols, d).

The idea is local:

  • visit one neuron
  • find its neighbors on the map grid
  • compute the distance to each neighbor
  • average those distances

The result is one scalar per neuron, which gives you a two-dimensional matrix you can visualize as a heatmap.

Compute Neighbor Distances

For a rectangular SOM grid, the simplest neighborhood is the four direct neighbors: up, down, left, and right. Some implementations include diagonals, but whichever choice you make should match the topology you want to visualize.

Here is a basic NumPy implementation:

python
1import numpy as np
2
3
4def build_u_matrix(weights: np.ndarray) -> np.ndarray:
5    rows, cols, _ = weights.shape
6    u_matrix = np.zeros((rows, cols), dtype=float)
7
8    for row in range(rows):
9        for col in range(cols):
10            distances = []
11
12            for d_row, d_col in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
13                n_row = row + d_row
14                n_col = col + d_col
15
16                if 0 <= n_row < rows and 0 <= n_col < cols:
17                    diff = weights[row, col] - weights[n_row, n_col]
18                    distances.append(np.linalg.norm(diff))
19
20            u_matrix[row, col] = np.mean(distances)
21
22    return u_matrix

That function produces a U-matrix with one value per neuron. Higher values indicate stronger local separation from neighboring neurons.

Example With a Small SOM

The following example uses a small synthetic weight grid so you can see the computation without needing a full training pipeline.

python
1import numpy as np
2
3weights = np.array(
4    [
5        [[0.0, 0.1], [0.1, 0.2], [0.2, 0.2]],
6        [[0.0, 0.0], [3.0, 3.0], [3.2, 3.1]],
7        [[0.1, 0.0], [3.1, 3.2], [3.3, 3.2]],
8    ],
9    dtype=float,
10)
11
12u = build_u_matrix(weights)
13print(np.round(u, 2))

In this toy example, the map contains a clear jump between small values near the top-left region and larger values near the lower-right region. The U-matrix highlights that transition as a ridge of high distances.

Visualize It as a Heatmap

The most common presentation is a heatmap.

python
1import matplotlib.pyplot as plt
2
3plt.imshow(u, cmap="viridis")
4plt.colorbar(label="Average neighbor distance")
5plt.title("U-matrix")
6plt.show()

Interpretation is usually:

  • low-distance areas suggest locally similar neurons
  • high-distance areas suggest boundaries between groups

Many practitioners also overlay sample hit counts, labels, or best-matching-unit assignments to make the clustering pattern easier to interpret.

Preprocessing and Topology Matter

A U-matrix reflects the geometry of the trained map, so if the training setup is poor, the visualization will also be poor. Two details matter especially:

First, feature scaling matters. If one input feature has a much larger numeric range than the others, Euclidean distance will be dominated by that feature. Standardizing inputs before training often makes the resulting U-matrix much more meaningful.

Second, neighborhood structure matters. A hexagonal SOM does not have the same neighbor pattern as a rectangular one. If your SOM library uses hexagonal topology, then a four-neighbor rectangular calculation is not the right visualization rule.

Library Support and Manual Implementation

Many SOM libraries already provide a helper for this. Even if your library does, implementing the U-matrix manually once is still useful because it clarifies what the plot actually means. It is not a mysterious extra model output. It is simply local average distance on the neuron grid.

That understanding helps you debug odd-looking maps. If the U-matrix appears noisy everywhere, the issue may be insufficient training, poor scaling, or a map that is too small for the data structure you are trying to visualize.

Common Pitfalls

The first pitfall is computing distances to every neuron in the map. A U-matrix is based on local neighbor distances, not global all-to-all distances.

Another issue is forgetting to normalize or standardize input features before training. Since the U-matrix uses distance, bad scaling can distort the entire visualization.

Developers also mix topologies by training one kind of SOM and visualizing it with the neighbor rules of another.

Finally, do not treat the U-matrix as a formal proof of cluster count. It is a visualization aid, not a replacement for careful analysis.

Summary

  • A U-matrix stores the average distance from each SOM neuron to its neighbors.
  • It is built from trained SOM weight vectors, not from raw input samples directly.
  • High values often indicate boundaries between locally different regions on the map.
  • Heatmaps are the usual way to visualize the result.
  • Correct preprocessing and topology-aware neighbor selection are essential for useful output.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.