Intro to Applied Topological Data Analysis | by Ryan Duve | Towards Data Science

Topological data analysis, or TDA, is a set of approaches providing additional insight into datasets. It augments other forms of analysis, like statistical and geometric approaches, and is useful to any data scientist that wants a more complete understanding of their data.
This article paints a picture of the utility of the topological approach, outlines a prominent TDA algorithm called “mapper”, and describes how to use a toolkit called Scikit-TDA for application to natural language processing. Finally, we share a specifics of applying all the above to the problem of training data collection for a machine learning pipeline involve documents from the financial services industry.
Topology is the math concerning continuous objects. Not sizes and shapes, but just continuity. It is about how things are connected and where the gaps are.
A simple analogy demonstrating why topological data inspection is so useful is putting up holiday lights. When tasked with installing holiday lights on a house, it is natural to first think about the problem geometrically and statistically.
Geometry answers questions like how long is the house, how high is the roof, where are the outlets and how many lights are needed. Statistics answers questions like how much current will the lights draw, how much do they cost, and how many hours a day will they be lit on average.
After installation comes the big moment to plug in the lights for the first time and... nothing. Suddenly, the lengths and dimensions and colors don’t matter at all. How much they cost and how heavy they are do not factor into why they are not turning on. Instead, we jump to troubleshooting mode and think about strings of lights in a totally different way. We think of them as circuits.
If only a few bulbs don’t come on, the string is in parallel and we just have to replace those broken bulbs. If none of the lights come on, they might all be in series and there could just be a handful of broken bulbs we have to find. Or we could have series-strings plugged into parallel-strings together in series. Or parallel! In any event, we are now positioned to start debugging with the above picture in mind, keeping in mind the schematics look nothing like the actual strings of lights on the house.
Looking at a topological visualization of a dataset is like looking at the circuit diagram of holiday lights. We intentionally ignore the spatial features, the giant lengths and short distances between points, and instead focus on only the connectivity of the data. We ask what is connected, what is separate, where are the gaps. This gives us new information to answer tough questions. It is important to remember TDA does not replace geometric and statistic analyses, but complements them the way a circuit diagram complements a tape measure when stringing up holiday lights on a house.
There are many excellent introductory videos explaining the mapper algorithm invented by Carlsson, Memoli and Singh in 2007. Instead of trying to replicate those resources, a skeletal overview is as follows:
Start with data in a high dimensional space.Create overlapping bins of data.Within each bin, cluster the data.For each cluster, create a node in a graph.Connect the nodes that share data points.
Start with data in a high dimensional space.
Create overlapping bins of data.
Within each bin, cluster the data.
For each cluster, create a node in a graph.
Connect the nodes that share data points.
In real data analysis, the original data is high dimensional and cannot be visualized the way the initial data points are here. Otherwise, we would be done! The resulting graph, expressed as nodes and edges, allows us to see topological relationships without concern for the geometric details of the initial data vectors.
Answer: Scikit-TDA.
Scitkit-TDA is a Python package that provides standard interfaces for previously independent algorithms. Like scikit-learn, methods in the family of .fit() and .fit_transform() are used to execute algorithms and transform data. An example looks like
import kmapper as kmmapper = km.KeplerMapper(verbose=2)projected_data = mapper.fit_transform(data, projection=TSNE())graph = mapper.map(    projected_data,    clusterer=DBSCAN(),    cover=km.Cover(35, 0.4),)
where TSNE and DBSCAN can be swapped with appropriate scikit-like objects with .fit() and .transform() methods of their own. For more detailed examples, see the Scikit-TDA example page.
Creating vectors out of text is important for machine learning models, which only know how to operate on lists of numbers. One of the simplest and easiest approaches is the bag of words model, where every unique word in a dataset gets its own direction in some high dimensional space and a document is represented by the coordinates of the words it contains.
Following the Scikit-TDA’s KepplerMapper example, we vectorize the documents’ text using TFIDF scaling, reduce down to two dimensions and use a clustering algorithm to build up the graph.
mapper = km.KeplerMapper(verbose=2)projected_X = mapper.fit_transform(term_count_matrix,    projection=[TfidfTransformer(sublinear_tf=True),                TruncatedSVD(n_components=100, random_state=0),                Isomap(n_components=2, n_jobs=-1)],    scaler=[None, None, MinMaxScaler()])
We made a few tweaks from the example in the documentation. Since we are dealing with so many large files, we use a custom parallel term counter with bounded memory to build up a term matrix, and then use a TFIDF transformer in the pipeline. We also experimented with different forms of dimensionality reduction, such as PCA, to build the projected data.
Finally, to build the graph:
graph = mapper.map(projected_X,    clusterer=AgglomerativeClustering(        n_clusters=3,        linkage="complete",        affinity="euclidean"),    cover=km.Cover(n_cubes=12, perc_overlap=0.33))
Again, we experimented with different clustering algorithms such as DBSCAN and HDBSCAN as well as changing the affinity and overlap parameters.
Financial documents are at the heart of many workflows at S&P Global. Internally, machine learning classification models are used to prioritize the flow of documents for many use cases. Training data are regularly provided by content experts within the company to ensure models are operating at peak performance.
A simplification of the training data collection process is as follows. For a given workflow, a content expert provides a “positive” or “negative” tag for each ingested document. At training time, we retrieve text for each document and train a classifier to attempt to linearly separate documents based on a model confidence score. When building a new model, we might see considerable (undesirable) overlap between positive and negative documents.
For a new workflow, a reasonable explanation is a problem with the training data labeling. Looking at files the model got wrong, somewhere around the middle, is a good place to start. For a sense of scale, there are about 9000 documents between the scores 0.0 and 1.0 on the x-axis.
We now use TDA to group the documents based on their topological properties and look for patterns in classification errors. We want to learn if documents that confuse the model are topologically connected. If so, we can use clustered nodes with high confusion as candidates to send back to content experts for additional consideration.
Applying TDA to the documents as described in the previous section, we end up with nodes and edges colored by average document confusion.
In the image, each circle is a cluster of documents. The smallest circles represent only a few documents, while the biggest represent several thousand documents. The brightest yellow balls contain only documents classified correctly by the model with confidence, while the deep purple contain only incorrectly classified documents. The spectral range from yellow to purple is the continuum of “average correctness” for each circle.
Incorrectly classified document groups, the blue and purple nodes, seem to appear mostly in the north and northwest regions of the image. We can select all documents from those nodes, look up their model confidence scores and see where they are on the model confidence curve.
Each green vertical bar represents one document from the blue/purple nodes of the TDA output, and a darker green section indicates a higher concentration of documents at that model confidence score. There are a little under 500 documents flagged by TDA, mostly between scores of 0.0 and 1.0 on the x-axis.
Before the TDA analysis, we had about 9000 documents to start looking at to determine if something was wrong with our data collection. That is now shrunk down to less than 500. This is not to say we could not have achieved gains with statistical or geometric methods, but we feel confident the documents we are sending back are related. With any luck, our business partners will find a pattern in the documents we send and correct a systematic error in training data collection, allowing for a better second round of model training.
If statistical methods are a data scientist’s measuring tape and making plots is the ladder, then TDA is the multimeter. It provides an orthogonal way to look at data by focusing on connections and gaps instead of size and shape. The linked resources provide a data scientist just getting started with TDA to get up to speed and be productive in a short time.
Scikit TDA| SciPy 2019 | Nathaniel Saul
“Topological Data Analysis for the Working Data Scientist” — Anthony Bak
TDA Mapper Part 2, Isabel K. Darcy
The mapper algorithm and Reeb graphs
An Introduction to Mapper, Bala Krishnamoorthy (2/11/2020)
Sliding Windows And Persistence, Perea and Harer
Scikit-TDA Github
Scikit-TDA documentation
Topological Data Analysis of Financial Time Series, Gidea and Katz
UMAP, McInnes, Healy and Melville
Extracting insights from the shape of complex data using topology, Carlsson et al
Topological Methods for the Analysis of High Dimensional Data Sets and 3D Object Recognition, Carlsson, Memoli and Singh
KeplerMapper 2.0.0 documentation
Applied Topology
An Introduction to a New Text Classification and Visualization for NLP Using TDA, Elyasi and Moghadam
Ryan Duve is a Data Scientist at S&P Global. He’s also a physicist, Linux hacker and a bad barista.