Anomaly Detection: Finding Outliers and Unusual Patterns
Learn how to detect anomalies and outliers using statistical and ML methods. Covers Z-score, IQR, Isolation Forest, LOF, and best practices for imbalanced anomaly data.
Anomaly Detection: Finding Outliers and Unusual Patterns
Anomaly detection identifies data points or patterns that deviate significantly from the norm. It is critical for fraud detection, intrusion detection, quality control, and health monitoring.
---
What Is Anomaly Detection?
Anomalies are rare, unusual, or unexpected observations. Types include:
| Type | Description | Example |
|---|---|---|
| Point anomalies | Single observation is unusual | One fraudulent transaction |
| Contextual anomalies | Unusual in a specific context | High temperature in winter |
| Collective anomalies | A subsequence or group is unusual | DDoS attack pattern |
Use Cases
| Domain | Application |
|---|---|
| Fraud | Credit card, insurance, identity theft |
| Security | Intrusion detection, unusual login patterns |
| Manufacturing | Defect detection, quality control |
| Healthcare | Disease onset, irregular vitals |
| Finance | Unusual trading, market manipulation |
| IT | Server failures, performance degradation |
Statistical Methods
Z-Score
Measures how many standard deviations a point is from the mean. |Z| > 2 or 3 typically flags outliers.
- Pros: Simple, interpretable.
- Cons: Assumes normal distribution; sensitive to extreme values (mean and std).
- Use when: Data is roughly normal; univariate.
IQR (Interquartile Range)
- IQR = Q3 − Q1.
- Outliers: below Q1 − 1.5×IQR or above Q3 + 1.5×IQR.
- Pros: Robust to extreme values; no distribution assumption.
- Cons: Univariate only; 1.5 is arbitrary.
- Use when: Skewed or heavy-tailed data.
Grubbs Test
Statistical test for a single outlier in normally distributed data. Returns a p-value.
- Use when: You want a formal test and expect one main outlier.
ML Methods
Isolation Forest
- Builds random trees; isolates points by randomly splitting on features.
- Anomalies need fewer splits to isolate → shorter path length → higher anomaly score.
- Pros: Fast, works well with high dimensions, no distance assumptions.
- Cons: Can struggle with local density structure.
- Use when: High-dimensional data, mixed types, need speed.
LOF (Local Outlier Factor)
- Compares local density of a point to its neighbors.
- Low density relative to neighbors → high LOF → anomaly.
- Pros: Handles local density; finds outliers in varying-density regions.
- Cons: Slower; sensitive to k (number of neighbors).
- Use when: Clusters with different densities; local anomalies matter.
One-Class SVM
- Learns a boundary around "normal" data in feature space.
- Points outside the boundary are anomalies.
- Pros: Flexible boundary; works with kernels.
- Cons: Sensitive to hyperparameters; slower on large data.
- Use when: Clear boundary between normal and anomalous; moderate data size.
DBSCAN
- Density-based clustering; points in sparse regions are noise (outliers).
- Pros: No preset number of clusters; finds arbitrary shapes.
- Cons: eps and minsamples tuning.
- Use when: You also want clustering; outliers are in low-density regions.
Choosing a Method
| Factor | Recommendation |
|---|---|
| Univariate | Z-score, IQR |
| Multivariate | Isolation Forest, LOF, One-Class SVM |
| Labeled anomalies (rare) | Supervised classification if you have enough examples |
| No labels | Unsupervised: Isolation Forest, LOF |
| Large data | Isolation Forest (scalable) |
| Local structure matters | LOF |
| Interpretability | Statistical methods, tree-based (feature importance) |
Evaluation
Anomaly detection is hard to evaluate because:
- Labels are often scarce.
- Class imbalance is extreme (most points are normal).
- Different costs for false positives vs false negatives.
- Precision: Of flagged points, how many are truly anomalous?
- Recall: Of true anomalies, how many did we catch?
- F1: Balance of precision and recall.
- ROC-AUC / PR-AUC: PR-AUC often better for imbalanced data.
Handling Imbalance
Most data is normal. Strategies:
| Strategy | Description |
|---|---|
| Adjust threshold | Tune decision threshold on anomaly score |
| Oversample anomalies | If you have some labeled anomalies |
| Use class weights | Weight anomaly class higher in supervised models |
| Focus on precision/recall | Choose metric by business cost |
| Use PR curve | Better than ROC for severe imbalance |
Time Series Anomaly Detection
- Point: Single timestamp anomalous (e.g., Z-score on values).
- Contextual: Unusual given time context (e.g., seasonal decomposition, then residual anomalies).
- Collective: Unusual pattern over window (e.g., sliding window + Isolation Forest).
---
Using Anomaly Detection in the Platform
- Select dataset and features.
- Choose method (Isolation Forest, LOF, Z-score, etc.).
- Set parameters (e.g., contamination, nneighbors).
- Run and inspect flagged points.
- Validate with domain knowledge; iterate on method and threshold.
Common Pitfalls
| Pitfall | Solution |
|---|---|
| Ignoring scaling | Scale numeric features before distance/density methods |
| Using accuracy as metric | Use precision, recall, F1, PR-AUC |
| One-size-fits-all threshold | Tune threshold for your use case |
| Treating all anomalies as equal | Consider severity; use weighted metrics |
| Not validating | Always spot-check results with domain experts |
| Over-flagging | Balance recall vs operational cost of false alarms |
Quick Reference
| Method | Best For | Key Parameters |
|---|---|---|
| Z-score | Univariate, normal-ish | Threshold (2 or 3) |
| IQR | Univariate, robust | Multiplier (1.5) |
| Isolation Forest | Multivariate, scalable | contamination, nestimators |
| LOF | Local density, multivariate | nneighbors |
| One-Class SVM | Clear boundary | nu, kernel |
| DBSCAN | Clustering + noise | eps, min_samples |
What's Next?
- [Clustering Overview](/analytics/user-manuals/adv-all/clustering-overview) — DBSCAN for anomaly + clustering
- [Statistics Overview](/analytics/user-manuals/stats-overview/statistics-overview) — Z-score and IQR foundations
- [Data Preprocessing Guide](/analytics/user-manuals/adv-all/data-preprocessing-guide) — Scaling and preparation
- [Visualization Overview](/analytics/user-manuals/viz-charts/viz-overview) — Visualizing anomalies
Related Articles
Clustering Analysis
Group similar data points together using K-Means, DBSCAN, Hierarchical, and other clustering algorithms.
Statistics Overview: What Are Statistics and How to Use Them
A beginner-friendly introduction to statistics, descriptive vs inferential analysis, the Statistics workspace layout, and running your first analysis.
Data Preprocessing: Cleaning, Transforming, and Preparing Your Data
Learn how to clean messy data, handle missing values, encode categorical variables, normalize numbers, and prepare your dataset for analysis or machine learning.
Visualization Overview: Charts, Types, and When to Use Them
Why visualize data, all chart types with "when to use" guidance, chart configuration, exporting, and a decision table for choosing the right chart.
Was this helpful?