# The Best Anomaly Detector I Know Optimizes Nothing

> Source: <https://dev.to/nishant_banginwar_80b7dc5/the-best-anomaly-detector-i-know-optimizes-nothing-1lck>
> Published: 2026-08-28 03:23:05+00:00

*Classic Machine Learning Through the Eyes of an SRE — Part 9: Isolation Forest*

**The algorithm in one line:** Isolation Forest scores how anomalous a point is by how few random cuts it takes to separate that point from everything else. No model of normal, no loss function, nothing optimized.

← Previous: [Part 8 — Hierarchical Clustering Fails Beautifully](https://dev.to/nishant_banginwar_80b7dc5/hierarchical-clustering-fails-beautifully-561b) · Next: this is the series finale — [start at Part 1](https://dev.to/nishant_banginwar_80b7dc5/logistic-regression-doesnt-make-decisions-your-business-does-3p0a).

Every anomaly detector I had studied models what NORMAL looks like, then calls the leftovers outliers. K-Means: far from every centroid. DBSCAN: in the noise bucket. Sensible, and intuitive.

Isolation Forest does not bother. It never models normal at all. It goes straight at the rare points with a single question: how few random cuts does it take to isolate you?

Random cuts, literally. Pick a feature at random, pick a split value at random between that feature's min and max, repeat. A point that separates from the crowd in three cuts is anomalous. A point buried in the middle of a dense mass takes thirty. Grow hundreds of these random trees, average the isolation depth for each point, and you get an anomaly score.

There is no loss function here. No optimization, not even the local kind that decision trees do at every split. Every cut is a coin flip, and the power comes entirely from averaging, which is the forest trick from the supervised half of this series now applied to pure randomness. Cheap randomness plus averaging beats careful modeling, as long as the target is something randomness naturally exposes. Rarity is exactly that.

Sometimes the winning move is to optimize less. That sentence would have gotten me laughed out of my first ML study session. It is also this finale's thesis.

Here is the thing I did not know until I read the original paper properly, and it is the opposite of every instinct a decade of ops gave me.

**Isolation Forest deliberately trains each tree on a small subsample of your data, and this is not a performance shortcut.** The common default is 256 points per tree, regardless of whether your dataset has ten thousand rows or ten million. My first assumption was that this was the usual accuracy-for-speed trade, the sort of compromise you accept and then apologize for in the design doc.

It is not a compromise. The paper's key insight is that you do not need the entire dataset to isolate anomalies effectively. A small subsample keeps rare points rare.

The reason sample size matters at all is that isolation is relative: an anomaly is interesting because it is easier to separate than the points around it. A small subsample keeps the forest focused on that isolation problem, while making the trees dramatically cheaper to build.

So the algorithm that optimizes nothing also, on purpose, looks at less of your data than you gave it. Every scaling instinct I have says that has to be a compromise. It isn't.

Isolation Forest outputs a ranking, a score per point, not a decision. Someone still has to draw the line.

That knob is contamination, and it is worth being precise about what it actually does, because I had this slightly wrong too. Contamination is not the score threshold itself. It is your stated estimate of what fraction of the data is anomalous, and the library uses it to derive the cut point from the observed score distribution. Set contamination to 0.01 and you have not said "flag anything above 0.7," you have said "roughly one percent of this is bad, go find that one percent."

Which means there is no universal statistical rule that tells you the right contamination value for your business — no elbow, no knee, no gap in a plot that settles it for you. It is a business call: how many investigations can the team absorb in a week, and what does a missed anomaly cost when it gets through? Every other algorithm in this series handed you at least a heuristic to fight about. This one hands you a mirror.

One practical note on reading the scores. The original paper's score runs from 0 to 1, where values near 1 are anomalous and values near 0 are firmly normal. Scikit-learn reports it differently, and the difference has bitten people. Its `score_samples`

is the opposite of the paper's score, so more negative means more anomalous. `decision_function`

then shifts that score by an offset so negative values are treated as outliers; when you specify contamination, that offset is chosen to produce the expected proportion of training outliers. Same ranking either way, inverted convention. I have watched that flip cost someone an afternoon.

A timesheet anomaly from an intern and one from a program lead can score identically strange. One is a rounding error, the other is revenue leakage. The fix is to re-rank by expected value, score multiplied by business impact, so investigation effort follows expected cost rather than statistical novelty. The algorithm ranks weirdness. Only the human side knows what weirdness costs.

By this point in the series the pattern has fully crystallized. The less an algorithm assumes and optimizes, the more the human setup and operation *is* the intelligence. Isolation Forest has no conventional optimization objective, and correspondingly, everything that makes it useful in production, the features, the threshold, the value weights, the triage queue, is human judgment wearing a model's clothes.

My worst mistake of the entire week happened here. I wrote in my notes that repeated missed breaches would be "properly handled" by Isolation Forest. That is precisely backwards, and it has a name.

**Masking** is the flagship failure. When anomalies repeat and clump together, they stop being few. A dense little cluster of the same fraud pattern now takes many cuts to isolate, scores as normal, and disappears. Translated for ops: the first occurrence of a new failure mode lights up beautifully. By the tenth occurrence, that *same* pattern may no longer look isolated — exactly when it has become systemic. Isolation Forest is an early-warning instrument, not a recurring-problem detector. Pair it with signature-based detection for known patterns, the same way you pair anomaly alerts with static thresholds in monitoring.

**Swamping** is the mirror image, and the one I had been describing without knowing the word for it. Normal points that happen to sit near an anomalous region get flagged as anomalous themselves, and your investigation queue fills with statistically strange but entirely harmless records. Both failures are silent. Neither appears in any metric the algorithm produces.

The small subsample is deliberate: it preserves the algorithm's isolation-based view of rarity while keeping the forest cheap to build. It does not eliminate masking or swamping.

One more limitation, and this one is structural rather than operational.

Every cut is axis-parallel, because each split uses exactly one feature at a time. That means the regions the forest carves out are axis-aligned rectangles, and the resulting score map inherits that shape. In practice this produces artifacts: bands and corners of the feature space that score as suspiciously normal even though no training data ever sat there. If your two features are correlated, so the real data lies along a diagonal, the rectangles fit that diagonal badly and the score surface develops ghost regions.

The named answer is Extended Isolation Forest, which cuts with randomly oriented hyperplanes instead of axis-parallel splits and reduces the artifacts caused by axis-parallel splitting. This is the same shape of lesson as Part 6, where DBSCAN's single density bar had HDBSCAN waiting behind it. The base algorithm makes a geometric assumption you can inherit without ever noticing, and someone has already written the version that relaxes it.

When this series started I claimed the algorithm is the last decision you make. The unsupervised half sharpened that into something stronger: the algorithm was never the intelligence at all. Framing the question, pricing the failures, choosing the features, setting the thresholds, deciding what matters — that was the intelligence the whole time. The algorithm is the part that scales it.

Ten years of ops taught me to distrust any system whose operator cannot say what it is actually doing. ML systems turn out to be no exception. They just hide it better, and they hide it behind a number that looks like an answer.

The output is a ranking, not a decision. Contamination is your estimate of the anomalous fraction, not a score cutoff, and no universal rule sets it for you, so it is a capacity-and-cost call. Re-rank by expected value so effort follows money rather than novelty. Keep the subsample small, because the default is deliberate. Pair it with signature detection for known repeat patterns.

Assuming it catches all anomalies. Repeated anomalies MASK: once a pattern clumps it stops being few, takes more cuts to isolate, and can score as normal exactly when it has become systemic. Its mirror, swamping, floods the queue with harmless neighbors of real anomalies.

Two more that separate people who have run it from people who have read about it. Believing the small subsample default is merely a speed compromise, rather than part of how Isolation Forest exploits rarity. And calling it a clustering algorithm; it produces per-point scores, not groups.

Early warning on novel failure modes in infra telemetry, timesheet and expense anomalies, fraud triage queues, SLA-breach precursors. Always with a human triage step in front of any action, and never as the only detector for a failure mode you have already seen.

*Classic Machine Learning Through the Eyes of an SRE. What each algorithm bets about your world, read through a production lens. Nine parts, ending here. Written while transitioning from a decade of SRE and DevOps into AI platform engineering, in public. If you disagree anywhere, comments are open. That is what they are for.*
