Notes

Affinity Matrix

When data points are easier to describe by how strongly they relate to each other than by their raw coordinates, an affinity matrix becomes the central object. It is a table that records pairwise similarity: each entry says how connected, alike, or close two data points are.

What it is

Technically, an affinity matrix is a square matrix where row i and column j contain a similarity score between point i and point j. Large values mean “these two belong together,” small values mean “they are weakly related.” In graph-based clustering, this matrix acts like the weighted adjacency matrix of a graph: data points are nodes, and affinities are edge weights. A common choice is the RBF (Gaussian) kernel, which turns distances into similarities, so nearby points get high affinity and distant points get low affinity. The diagonal is usually the self-similarity of each point, often set to 1 or 0 depending on the method.

Why it matters

In spectral clustering, the affinity matrix is the starting point for building the graph Laplacian, whose eigenvectors reveal cluster structure. This is what lets the method separate curved or intertwined groups that k-means would miss. The quality of the clustering depends heavily on how the matrix is built:

  • If affinities connect unrelated points, clusters blur together.
  • If affinities are too sparse or too local, true groups break apart.
  • The similarity rule and its scale parameter directly shape the graph structure.

Practical use

In customer segmentation, two shoppers might have high affinity if their browsing and purchase patterns are similar. In image segmentation, nearby pixels with similar color get strong affinity, helping isolate objects. In document analysis, texts sharing vocabulary or embeddings receive higher affinity, supporting topic discovery. In practice, tools like scikit-learn use this idea in SpectralClustering, where the affinity can be computed from nearest neighbors or an RBF kernel. The matrix is not just bookkeeping—it is the model’s view of which points belong together.

Affinity Matrix is a square matrix that stores the pairwise similarity between data points, where each entry quantifies how strongly two points are connected under a chosen similarity measure. It is the core representation for graph-based and spectral clustering, because cluster structure is inferred from these relationships rather than raw coordinates. If the affinity matrix is poorly constructed, the resulting graph and discovered clusters are unreliable.

Imagine a class photo where every pair of students gets a “how alike are these two?” score. Put all those scores into a big table, and you have an affinity matrix.

In AI, an affinity matrix is a way to record how strongly each item is related to every other item. High values mean “very similar” or “closely connected.” Low values mean “not much alike.” This matters in clustering, where the goal is to find natural groups without being told what the groups are first. Instead of looking at items one by one, the system looks at the whole web of relationships, which helps it spot groups that may be hard to see from raw data alone.