Learn AI Engineering Rent GPUs By The Hour Docker VPS Hosting Automate 3000+ Apps No Code AI Agents Proxies For Your Agents
Learn AI Engineering Rent GPUs By The Hour
Websites To LLM Data Proxies For Scraping AI Support Chatbot AI Data Analyst AI Agent Workspace Hire AI Builders

Isolation Forest: How It Works and How to Train One

Updated November 2026
An isolation forest detects anomalies by building 100 random trees and measuring how few cuts it takes to separate a row from all the others, since an unusual row gets isolated in a few cuts and a normal one takes many. It is the general purpose anomaly detector, it works with many columns at once, needs no scaling and no labels, and it is the one to try first.

How an Isolation Forest Decides

Most ways of finding anomalies start by describing what normal looks like and then measuring how far a row is from that. Isolation forest skips the description and goes straight at the odd rows, using 1 observation: unusual things are easy to separate from the crowd.

Picture your rows as dots and start cutting at random. Pick a random column, pick a random value between its lowest and highest, and split the data in 2 at that value. Pick again on each half and keep going until every dot is alone. A dot sitting in the middle of a dense crowd needs a lot of cuts before it is the only one left on its side, there is always someone else nearby. A dot way off by itself gets cut off almost immediately, often in 2 or 3 cuts. So the number of cuts it took to isolate a row is a score for how unusual it is, and fewer is weirder.

One random tree is a noisy measure, a normal row can get unlucky cuts. So the forest builds 100 of them and averages the cut count for each row. A new input gets run down all 100 trees, and if its average depth is short enough it is flagged. The answer you get back is 1 for anomaly and 0 for normal. So basically the model means "if a few random questions are enough to single you out, you are not like the others".

What an Isolation Forest Is Best At

  • Transactions and events with many columns. Amount, time of day, account age, item count, and location all at once, where no single column is odd but the combination is.
  • Not knowing what you are looking for. It needs no examples of the bad thing, which matters because the next problem is usually one you have not seen yet.
  • Raw mixed columns. The cuts are on 1 column at a time, so dollars and counts and flags need no scaling.
  • Large datasets. Each tree works on a small sample of the rows, so training stays fast as the data grows.
  • A first filter in front of something expensive. Flag the unusual 1% and only spend review time or model tokens on those.

Where It Breaks

  • Unusual only for its context. An order of 900 dollars is ordinary across your whole customer base and very strange for a customer who always spends 20. The forest looks at the whole dataset, so it misses that, and Local Outlier Factor catches it.
  • Anomalies that come in groups. If 50 fraud rows all look alike, they form their own little crowd and stop being easy to isolate.
  • It always flags a set share. The threshold comes from contamination, so it will flag that fraction of ordinary data even when nothing is wrong. More on that below.
  • No reason given. You get 1 or 0, with no word on which column made the row unusual. When the reason matters, Robust Z Score is the explainable one.
  • Trained on dirty data. If the training rows are full of the bad cases, those become part of normal.

Parameters

{"estimators": 100, "contamination": 0.1}
  • estimators is the number of random trees. 100 is plenty for nearly every case, more only makes the score a little steadier.
  • contamination is the share of rows you expect to be anomalies, and it is the setting that decides how the detector behaves. It does not change the scores, it sets the cutoff: at 0.1 the cutoff is placed so the most unusual 10% of the training rows would be flagged.

The default of 0.1 is high for most real uses, it means about 1 in 10 normal inputs comes back as an anomaly. If you process 5,000 transactions a day, that is 500 alerts, and nobody reads 500 alerts. Set it to the rate you can actually act on, 0.01 for 1 in 100, 0.001 for 1 in 1,000. The value has to be above 0 and at most 0.5.

How to Train an Isolation Forest Step by Step

These steps use ML Prediction Engine, the free open source tool this topic uses for its examples, where the class is named isolationForest. The machine learning models guide covers installing it.

1. Create a pipeline with the isolationForest class

Create a pipeline, add one model step, set the category to anomaly and the class to isolationForest. Choose numeric input for columns of values, which is the usual case, or text for messages.

2. Collect normal rows only

Pull a few hundred to a few thousand rows of ordinary behavior, a normal month of transactions, healthy server readings, legitimate signups. Take out the ones you already know were bad, confirmed fraud, the day of the outage. The cleaner the picture of normal, the sharper the detector.

3. Load the rows with no labels

An anomaly row is only an input:

curl -X POST http://localhost:8080/api.php/dataset/add \
  -H "Content-Type: application/json" \
  -H "X-API-Key: YOUR_KEY" \
  -d '{"pipelineID": 1, "modelIndex": 0, "input": [42.50, 14, 380, 2]}'

That row is a 42.50 dollar transaction at hour 14, from an account 380 days old, with 2 items. Send raw values in a fixed column order.

4. Set contamination to your expected alert rate

Decide how many alerts per day you can handle, divide by your daily volume, and use that. 20 reviews a day out of 5,000 transactions is 0.004. Set the parameters before training, since the cutoff is fixed when the model is built:

{"estimators": 100, "contamination": 0.004}

5. Train and test with known cases

Click Train or call api.php/train. Then send 20 ordinary inputs, nearly all should return 0, and send every known bad case you set aside in step 2, most should return 1. If the known bad ones come back 0, either they are not actually unusual on the columns you chose, so add a column that shows what was wrong with them, or the cutoff is too tight, so raise contamination a little and retrain.

6. Predict and act on the flag

curl -X POST http://localhost:8080/api.php/predict \
  -H "Content-Type: application/json" \
  -H "X-API-Key: YOUR_KEY" \
  -d '{"pipelineID": 1, "input": [2900, 3, 1, 14]}'

The answer is {"output": 1, ...} for an anomaly and 0 for normal. A 1 means "look at this", not "this is fraud", so route it to a review queue or a second check instead of blocking outright. Retrain on fresh normal data every so often, because what counts as normal drifts.

When to Use a Different Detector

If the anomalies you care about are only strange for a particular customer, region, or time, use Local Outlier Factor, which judges each row against its own neighborhood. If you are watching 1 or 2 numeric metrics and need to tell someone exactly why an alert fired, Robust Z Score gives a rule you can say in a sentence. And once you have collected a few hundred confirmed bad cases, you are no longer limited to anomaly detection, you can label them and train a classifier like Random Forest, which will be more accurate at finding that specific problem.

Key Takeaway

An isolation forest flags rows that random cuts separate quickly from the rest. Train it on normal data only, send raw columns with no scaling, and lower contamination from the default 0.1 to the alert rate you can really handle, because that setting decides how often it answers 1.