Skip to chapter content
navan✳THE BUILD LABMACHINE LEARNING / GUIDE
0/28 read
CHAPTER 24 / 28 · UNSUPERVISED AND TEXT

Topics, outliers, and hidden structure.

Read topic models and anomaly scores as exploratory signals rather than objective truth.

BY NAVAN SAMILONG READTHEORY FIRST
01 / FROM THE BEGINNING

Start with the meaning.

Unsupervised methods can ask more than “which cluster?” Topic modelling looks for recurring patterns of words across documents. A topic is often represented by words with high weights, such as “match, team, score” or “market, price, trade.” A document can mix several topics. The topic numbers have no intrinsic names; people interpret them after examining representative documents. A topic model does not discover the one true subject of every document.

Anomaly detection looks for examples that appear unusual relative to a reference dataset. An outlier might be a rare but legitimate case, a recording error, or an important event. An anomaly score expresses how unlike the training pattern a case appears under a chosen model. It is not automatically a probability of fraud or equipment failure. The difference between unusual and harmful is a domain decision requiring further evidence.

02 / THE MODEL

How the idea works.

Latent Dirichlet allocation and non-negative matrix factorisation are different topic-model families. Both summarize recurring word patterns under assumptions about how documents are generated or approximated. The number of topics is chosen by the analyst and affects interpretation. Short documents, stopword choices, and changes in vocabulary can make topics unstable. A useful topic report should show top words and example documents, not just a list of topic IDs.

Anomaly methods also make assumptions. Isolation Forest searches for cases that are easy to isolate through random splits. Local Outlier Factor compares local densities. A one-class model learns a region of normal-looking data. Each can respond differently to feature scaling, high dimensionality, and a dataset contaminated with anomalies. A threshold converts a continuous score into an alert and determines how many cases a human must review.

These tasks are hard to evaluate without trustworthy labels. If rare events eventually receive human review, use those outcomes carefully and account for selection bias: only cases that were flagged may have been investigated. A high alert rate can overload reviewers, while a low one can miss important events. Evaluate the whole workflow, including review capacity and the meaning of a false alarm.

03 / PYTHON, EXPLAINED

Follow the code.

topics-and-anomalies.py
from sklearn.ensemble import IsolationForest

X = [[0.0], [0.1], [-0.1], [0.2], [10.0]]
model = IsolationForest(contamination=0.2, random_state=0)
labels = model.fit_predict(X)
print(labels)
print(model.decision_function(X))

Four one-dimensional rows lie near zero and one lies far away at 10. IsolationForest fits a model of unusualness from these rows. fit_predict returns 1 for rows treated as inliers and -1 for rows treated as outliers. With contamination=0.2, the estimator is asked to mark roughly one in five training rows as outlying; the faraway value is the natural candidate here. decision_function gives relative scores under the fitted model, with more negative values indicating more anomalous cases by its convention.

The setting contamination is not a discovered true fraud rate. It encodes an assumption used to set the alert threshold. If the examples came from a process where rare 10.0 values were legitimate, the model would still flag them as unusual. The code detects a pattern in recorded numbers, not the real-world consequence of that pattern.

04 / THE BOUNDARY

What the result does not say.

Topic and anomaly outputs are useful for exploration and triage when their uncertainty is visible. Assigning a permanent category or punitive action from an unsupervised label alone can overstate what the algorithm knows.

A model of normality can become stale as products, customers, or sensors change. Drift monitoring and human feedback are needed to keep its alerts meaningful over time.

05 / KEEP THIS

One clear idea.

Unsupervised labels expose structure under assumptions; their practical meaning needs human and domain evidence.

THE CODE IN CONTEXT

The ML cookbook

Complete, explained Python workflows sit alongside this reading course.

Browse the cookbook
Finished reading?
Built with curiosity. By Navan Sami. ↗Machine learning with Python · From examples to trustworthy models.