The contamination rate is the expected proportion of anomalies in a dataset, expressed as a fraction such as 0.01 or 0.05, which an anomaly detection model uses to convert continuous anomaly scores into binary labels. Also called the expected outlier fraction, it tells the detector what share of the training data should fall above the decision threshold.
In anomaly detection, the term has two closely related uses. As a model parameter, it sets the threshold in libraries such as scikit-learn and PyOD. As a property of data, it describes how many anomalies actually contaminate a training set that is assumed to be mostly normal. Both meanings matter, because a mismatch between the assumed and the true contamination rate is a leading cause of poor detection quality.
How the contamination rate sets the threshold
Most unsupervised detectors, including the isolation forest, local outlier factor and k-NN detectors, output an anomaly score for each point rather than a label. To produce labels, the model sorts the training scores and places the threshold at the (1 minus contamination) quantile. With a contamination rate of 0.02, the 2 percent of training points with the highest scores are labeled anomalous, and new points are judged against the same fixed cut-off.
This mechanism means the parameter usually does not change the scores themselves. It changes only where the line is drawn. Two models with identical scoring but different contamination settings rank points in the same order and differ only in how many they flag. Metrics that ignore the threshold, such as ROC AUC and average precision, are therefore unaffected by the setting, while precision, recall and F1 depend on it directly.
The table below shows how the parameter value translates into flagged points on a training set of 100,000 records.
| Contamination setting | Points flagged | Threshold quantile |
|---|---|---|
| 0.001 | 100 | 99.9th percentile |
| 0.01 | 1,000 | 99th percentile |
| 0.05 | 5,000 | 95th percentile |
| 0.1 | 10,000 | 90th percentile |
How common libraries handle the parameter
Implementations differ in defaults and permitted ranges, and these differences explain many surprising results when a model moves between libraries. In scikit-learn, the isolation forest and local outlier factor accept a float or the value auto, which uses a fixed score offset instead of a quantile. PyOD detectors use a default of 0.1 and accept values up to 0.5, since a fraction above one half would make anomalies the majority.
The comparison below summarizes typical behavior. Exact defaults can change between library versions, so teams should confirm them in the installed version.
| Detector | Parameter behavior | Practical note |
|---|---|---|
| Isolation forest (scikit-learn) | Float or auto | Auto uses a fixed score offset |
| Local outlier factor (scikit-learn) | Float or auto | Novelty mode needed for new data |
| Elliptic envelope | Float, default 0.1 | Also shapes the robust covariance fit |
| One-class SVM | Uses nu instead | Nu bounds the training outlier fraction |
| PyOD detectors | Float, default 0.1 | Sets the threshold attribute after fitting |
In the one-class SVM, the parameter nu plays a similar but not identical role: it is an upper bound on the fraction of training errors and a lower bound on the fraction of support vectors, and it influences the learned boundary itself, not only the threshold.
Choosing a contamination rate
The best setting is the true anomaly prevalence in the data the model will see, but that prevalence is usually unknown. Practitioners estimate it from historical incident rates, labeled audit samples, domain knowledge or the review capacity of the team that handles alerts. When none of these exist, the default of 0.1 is rarely appropriate, because real anomaly rates in fraud, intrusion or sensor data are often well below 1 percent.
The decision table below matches common situations with a recommended approach.
| Situation | Recommended approach | Reason |
|---|---|---|
| Labeled sample available | Use the observed anomaly share | Matches real prevalence |
| Fixed alert capacity | Set rate to capacity over volume | Keeps the queue manageable |
| No prior knowledge | Rank by score, review top cases | Avoids an arbitrary cut-off |
| Prevalence drifts over time | Use dynamic thresholding | A fixed fraction goes stale |
| Clean training data assured | Set a very small rate | Novelty detection setting |
Contamination of the training data
Separately from the parameter, contaminated training data contains anomalies that were assumed absent. Semi-supervised methods such as autoencoder anomaly detection and Deep SVDD learn a model of normal behavior, and anomalies hidden in the training set teach the model to treat similar anomalies as normal. Reconstruction-based detectors are particularly sensitive, since an autoencoder can learn to reconstruct the contaminating patterns.
Robust methods reduce this effect. The minimum covariance determinant fits its estimate on the most concentrated subset of points, and the isolation forest tolerates moderate contamination because it subsamples the data. Iterative cleaning, which removes the highest-scoring training points and refits, can also help, but it risks removing rare yet legitimate behavior.
Common mistakes with the contamination rate
The following mistakes appear often in production anomaly detection systems.
- Keeping the library default without checking whether 10 percent anomalies is plausible for the data.
- Tuning the rate on test labels, which leaks information and inflates reported precision.
- Assuming the flagged fraction stays constant on new data, although score distributions shift over time.
- Comparing detectors at different rates, which mixes a threshold effect with genuine ranking quality.
- Treating the parameter as a confidence level, when it is only an assumed prevalence.
The idea in brief
An anomaly detector gives every record a suspicion score. It still needs a rule for where suspicion becomes an alert. The contamination rate is that rule. It says roughly what share of records you expect to be unusual.
Picture a teacher told to expect two cheaters in a class of one hundred. The teacher flags the two most suspicious students. If the guess is wrong, the teacher either misses cheaters or accuses honest students. A good estimate of the real share keeps both mistakes low.
A practical scenario
A payments team monitors about 400,000 card transactions per day with an isolation forest. The first version keeps the library-style setting of 0.1, so the model flags around 40,000 transactions daily. The two-person review team cannot inspect them, and confirmed fraud is a tiny share of the alerts.
Historical chargeback data shows a fraud prevalence near 0.1 percent, and the team can review about 500 cases per day. Analysts set the contamination rate to 0.00125, which produces roughly 500 alerts against the training score distribution. They track the daily alert count and recalibrate monthly. Precision on reviewed alerts rises sharply, and the team now spends its time on cases that are genuinely suspicious.