top of page

TDA: Machine Learning on Topological Features

Writer: Mic
Mic
May 15
7 min read

In the last post, we extracted holes and loops from point clouds. Now we have persistence diagrams or in other words scatter plots of topological features, each dot representing something that existed for a while before collapsing.

The question is: what do we do with them?

The problem here: Diagrams are not vectors, they are simply a set of points. You cannot feed them directly into a neural network or random forest. You need to turn them back into features.

I guess ChatGPT thinks this is somehow digital alchemy...or it is high.

The Problem: Diagrams Are Not Fixed-Size

Every sample might produce a different number of topological features. One point cloud gives you 5 loops, another 12, and another one 3.

But most standard machine learning expects fixed-size inputs. A neural network needs to know the input dimension before you start training.

This is why vectorization exists: methods to turn variable-length persistence diagrams into fixed-size feature vectors while preserving topological signal.

The trade-off is always the same: More expressive representations lose less information but are harder to compute, while simpler ones are fast but throw away detail.


Path 1: Persistence Images — Smooth Histograms

A persistence image is a 2D histogram of a persistence diagram, smoothed with a Gaussian kernel. We already saw some of this in the last post and go into some more detail here.

The basic idea is the following for a fixed chosen dimension:

  • Take all the features as pairs (birth, persistence), here persistence is the time they are alive, so death - birth.

  • Now subdivide the x-scale (birth) and the y-scale (persistent) into a grid.

  • Instead of simply collecting into which of those grid cells a feature falls we use a smooth bump, usually a Gaussian over the point and put that in the grid cells.

This procedure would mean that a point of the form (0.42, 0.70) would contribute to points close by, but less and less the further you go away from this. In addition the bump's height is determined by a weight that weights features higher the longer they are alive.

persim makes this straightforward:

Now you have fixed-size 2D arrays. You can flatten them into vectors and feed them into sklearn classifiers, or use them as inputs to a CNN.

The fundamental idea: Persistence images treat diagrams like probability distributions. You are asking “where is the topological mass concentrated” rather than “what are the exact features”.

This is done at a loss. If two different diagrams produce similar images, you cannot reconstruct the original features. But for classification tasks, that is often fine.


Path 2: Persistence Landscapes — Functional Summaries

Persistence landscapes turn diagrams into sequences of piecewise-linear functions. They are more interpretable than images but computationally heavier.

The idea: for each feature in the diagram, create a “tent” function that peaks at its midpoint and drops to zero at birth and death. Then take the top k functions at each point. The first landscape layer is the always uses the value of the top function, the second layer uses the second highest value and so on. Visually they will be nested inside each other.

Landscapes have a useful property: they live in a vector space. In the example above, you can either see each landscape as a collection of 5 vectors of 100 numbers each or as a single matrix of shape (5,100). In either case, you can add them, subtract them, compute averages. This makes them natural for statistical analysis.

If you want to know “what is the average topological shape of my dataset”, you compute the mean landscape across samples. You cannot do that with raw diagrams.

A side note here: There is a second method of production landscapes, which produces it more accurately in mathematical terms, PersLandscapeExact. Unlike PersLandscapeApprox, the exact persistence landscape is not stored as a regular array. It is stored as a list of critical pairs for each layer or in simple terms the series of points at which the layer makes a turn. Between those points, the landscape is obtained by linear interpolation. But not every layer will have the same number of critical points, making this method less ideal for getting fixed sized data.



The Pipeline: giotto-tda and scikit-learn Integration

If ripser is the fast engine and persim is the vectorization toolkit, then giotto-tda is the pipeline builder.

It wraps TDA computations in a scikit-learn-style API, so you can treat topological features like any other preprocessing step.

What just happened in this code:

  1. VietorisRipsPersistence computes persistence diagrams for each point cloud

  2. PersistenceEntropy extracts a scalar feature: the entropy of the diagram’s distribution

  3. RandomForestClassifier trains on those entropy values

You can swap PersistenceEntropy for other vectorization methods: Amplitude, BettiCurve, PersistenceImage, or Landscape. The pipeline structure stays the same.

In the example we trained the pipeline with two noisy circles, with a label of 1, and a noisy cloud, with a value of 0, and then testing on another noisy circle. The result is with my version was

Prediction: 1
Class probabilities: [0.04 0.96]

This is the real value of giotto-tda: it turns TDA into a composable step inside a standard machine-learning workflow. You do not need to manually manage intermediate outputs.


Comparing Shapes: Wasserstein and Bottleneck Distances

Sometimes you do not want features. You want to know: how similar are two shapes?

The bottleneck distance and Wasserstein distance are metrics on persistence diagrams. They measure how much you need to “move” one diagram to match another.

Bottleneck distance is the worst-case matching cost. It looks at the hardest-to-match feature and reports that distance.

Wasserstein distance is the average matching cost. It sums up all the matching costs and penalizes based on persistence.

Use bottleneck when you care about “is there any significant structural difference”. Use Wasserstein when you care about “how different are they on average”.

These metrics are useful for clustering shapes, testing stability, or building distance-based classifiers (like k-NN on persistence diagrams).


The Mapper Algorithm: From Points to Graphs

Mapper is not about persistence diagrams. It is a different TDA tool entirely: a way to visualize high-dimensional data as a graph.

The idea:

  1. Pick a “lens” function that maps your data to a low-dimensional space (often the first two PCA components)

  2. Cover that space with overlapping bins

  3. For each bin, cluster the points that map into it

  4. Create a graph where nodes are clusters and edges connect overlapping clusters

The result is a simplicial representation of your data that you can actually look at.

kmapper handles this:

Here we creates 1000 random points on two concentric circles, an inner and an outer one, and then adds 8 dimensions with random noise to them. Then we use a PCA mapper to reduce it back to 2 dimensions. The 2 dimensional space is covered by overlapping regions with mapper.map and the higher dimensional space clustered via DBSCAN. These clusters are turned into graph nodes connected if they share data points. Finally this is saved as an HTML document.

Open mapper_output.html in a browser. You will see an interactive graph where each node is a cluster and the structure reveals the “shape” of your data. Depending on the exact parameters it should look roughly similar to a ring with the inner and outer ring being the circles from the original 2 dimensional data points. The Mapper output preserves some of the topological structure even though the input was 10-dimensional and contained plenty of noise.

When to use Mapper:

When you want to explore high-dimensional data and do not yet know what questions to ask. Mapper is a visualization tool first, a modeling tool second.

Do not use Mapper when you already have a well-defined supervised learning task. It is too exploratory for that.


Combining Everything: TDA Features + Traditional Features

TDA does not replace standard features, it can easily complement them.

A common workflow could then look as follows: extract both persistence-based features and domain-specific features, then concatenate them.

This gives you both worlds: topological invariants that capture global structure and simple statistics that capture local properties. Of course for brevity we kept the statistical features rather simple here.

Worth noting: Sometimes topological features do not help. If your problem is purely local, like “classify this pixel as tumor or healthy tissue”, global topology might be irrelevant or even worse make the test completely unreliable. Test both with and without TDA features. If there is no indicator that the global shape of your data has any meaning, it might very well have no meaning.


Protein Binding Site Classification

Let’s build a "realistic" pipeline: Assume you have 3D coordinates of atoms in protein binding sites, and you want to classify whether a site binds to a specific ligand.

What makes this work:

  • Betti curves summarize how many connected components, loops, and voids exist at each distance threshold

  • The classifier learns that certain topological patterns (e.g., a cavity with a void) correlate with binding affinity

  • The pipeline is end-to-end: you feed in 3D coordinates, it outputs predictions

This is not a non-sensical example. TDA has been successfully applied to protein structure analysis, drug discovery, and materials science precisely because these domains have geometric structure that matters.


When NOT to Use TDA in Machine Learning

TDA adds computational cost, for example persistence computations are O(n³) in the worst case.

Do not use TDA when:

  • Your dataset is enormous, like millions of samples, unless you can subsample intelligently.

  • Your features are already low-dimensional and well-separated, just use logistic regression.

  • You have no reason to believe geometry matters, let's say you are looking at purely tabular data with no spatial relationships

  • You need real-time predictions (TDA is offline preprocessing, not online inference)

A reasonable heuristic: try a baseline model first. If it works well, you probably do not need TDA. Use TDA when you suspect the global structure of your data contains signal that local features miss. This is especially true if your data contains something like locations.


What We Covered

In this two-part series, we moved from the basics of persistent homology to practical machine-learning workflows.

Part 1 showed you how to compute persistence diagrams with ripser and GUDHI, and when to use each.

Part 2 showed you how to turn those diagrams into features with persim and giotto-tda, how to compare shapes with Wasserstein distances, and how to visualize high-dimensional data with Mapper.

The tools are mature and the math is solid. Which is not a surprise, homology is one of the oldest tools in modern topology. The question is whether your problem has the right kind of structure for topology to help.

If your data lives on or near a manifold, if there are loops, voids, or connected regions that matter, TDA gives you a way to extract those patterns as features. If not, it will politely give you noise and you should use something else.

Comments


bottom of page