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

Local Outlier Factor (LOF): How It Works and How to Train One

Updated November 2026
Local outlier factor detects anomalies by comparing how crowded the area around a row is with how crowded the areas around its nearest neighbors are, and flagging the row when it sits somewhere much emptier than they do. It is the detector for values that are only strange in context, a number that is perfectly ordinary across the whole dataset and clearly wrong for the kind of case it belongs to.

How Local Outlier Factor Decides

The key word is local. A detector like Isolation Forest asks 1 question for the whole dataset, "is this row far from everything?". Local outlier factor asks a narrower one, "is this row far from the things it should be near?".

Here is the case where that difference shows. Say you have 2 kinds of customers. Small accounts place orders that are all tightly packed between 15 and 30 dollars. Enterprise accounts place orders spread loosely anywhere from 2,000 to 20,000. Now a small account places a 90 dollar order. Across the whole dataset 90 is nothing special, it sits between the 2 groups and is much closer to ordinary small orders than the enterprise orders are to each other. But for a small account it is 3 times anything seen before.

LOF catches it like this. For the new row it finds the k nearest rows, 20 by default, and measures how far away they are, that is how crowded the row's own neighborhood is. Then it does the same for each of those 20 neighbors. The neighbors are small account orders packed tightly together, so their neighborhoods are very dense. The new row had to reach a long way to find them, so its neighborhood is sparse. Its density divided into theirs is the outlier factor, around 1 means "as crowded as my neighbors, I fit in", and well above 1 means "my neighbors are packed together and I am out here alone". Meanwhile an enterprise order of 14,000 sits among neighbors that are just as spread out as it is, so it scores near 1 and passes. So basically the model means "compare me to my own kind, not to everyone".

What Local Outlier Factor Is Best At

  • Data with several kinds of normal. Different customer tiers, product categories, regions, or times of day, each with its own typical range.
  • Values wrong for their context. The small account with the large order, the night shift reading that would be fine at noon.
  • Groups of different tightness. Some of your normal is very consistent and some is loose, and 1 global threshold can not serve both.
  • Smaller datasets. It works with a few hundred rows as long as each kind of case has enough of them.
  • Raw columns. The engine scales the columns for this class automatically, so mixed units go in as they are.

Where It Breaks

  • Speed on large data. Like every neighbor based model it compares the input to stored rows on each prediction, so it slows down as the dataset grows. Past tens of thousands of rows, isolation forest is the practical choice.
  • Many columns. With dozens of columns everything is far from everything, "nearest" stops meaning much, and the scores flatten out.
  • Junk columns. All columns count toward the distance, so ones that do not matter hide the ones that do.
  • Small groups of normal. If a legitimate kind of case has fewer rows than k, its members have to reach into other groups for neighbors and get flagged for it.
  • A set share is always flagged. As with isolation forest, contamination fixes the cutoff, so some ordinary inputs come back as 1.

Parameters

{"k": 20, "contamination": 0.1}
  • k is how many neighbors define a neighborhood. 20 is the standard starting point. Smaller values like 5 or 10 make it sensitive to very fine detail and jumpy, larger values like 40 smooth things out and start blending separate groups together. The rule that matters: keep k smaller than the number of rows in your smallest legitimate group, or that whole group looks like outliers.
  • contamination is the share of rows expected to be anomalies, and it sets where the cutoff goes. At the default 0.1 about 1 in 10 ordinary inputs is flagged, which is far too many for most uses. Set it to the rate you can act on, 0.01 or 0.005, anywhere above 0 and up to 0.5.

How to Train a Local Outlier Factor Detector 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 localOutlierFactor. The machine learning models guide covers installing it.

1. Create a pipeline with the localOutlierFactor class

Create a pipeline, add one model step, set the category to anomaly, the class to localOutlierFactor, and the input type to numeric.

2. Include the columns that define the context

This is the step that makes the model work. The value you are checking has to travel with the columns that say what kind of case it is, otherwise there is no context to be local to. For the order example send the order amount together with the customer's average order over the last 90 days and their order count, so a small account's orders sit near other small account orders. 3 to 8 columns is the comfortable range.

3. Load normal rows from every kind of case

Rows are inputs only, and they should be normal behavior. Make sure every kind of case is represented with at least 2 to 3 times k rows, so 40 to 60 each at the default.

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": [22, 24.5, 31]}'

That row is a 22 dollar order from a customer who averages 24.50 across 31 orders.

4. Set k and contamination

Count the rows in your smallest kind of case and keep k under that. Then set contamination from the number of alerts you can handle divided by your volume. Both are fixed at training time:

{"k": 20, "contamination": 0.01}

5. Train and test with known cases

Click Train or call api.php/train. Test each kind of case separately: ordinary small account orders should return 0, ordinary enterprise orders should return 0, and a small account with an order several times its usual size should return 1. If a whole legitimate group keeps coming back 1, it has too few rows for the current k, add rows for it or lower k.

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": [90, 24.5, 31]}'

The answer is {"output": 1, ...} for an anomaly and 0 for normal. Treat a 1 as a reason to look, and send it to a review step. Refresh the dataset with recent normal rows on a schedule so the picture of each group stays current.

Local Outlier Factor or Isolation Forest?

Use Isolation Forest when normal is basically 1 thing, the dataset is large, or there are many columns, it is faster and less fussy. Use LOF when normal comes in several kinds with different ranges and the anomalies you care about hide between them. If you can not tell which describes your data, train both on the same rows in 2 pipelines and send your known bad cases to each, the one that catches more of them at the same alert rate wins. For a single metric where the reason has to be stated plainly, Robust Z Score is simpler than either.

Key Takeaway

Local outlier factor flags a row when its neighborhood is much emptier than its neighbors' neighborhoods, so it catches values that are only wrong for their context. Send the context columns with the value, keep k below your smallest normal group, and lower contamination to an alert rate you can act on.