Outlier Detection Methods Examples¶
Examples demonstrating 9 outlier detection methods for quality control, anomaly identification, and data cleaning.
Methods by Complexity¶
Beginner (simple, deterministic):
outlier_absolutelimits.py — Enforce physical min/max constraints
outlier_manualremoval.py — Explicitly flag known problematic timestamps or periods
outlier_trim.py — Symmetric trimmed mean approach (TrimLow)
Intermediate (statistical, adaptive):
outlier_hampel.py — Median Absolute Deviation (MAD) in rolling windows
outlier_zscore.py — Z-score thresholding (global, rolling, day/night, increments)
outlier_localsd.py — Local standard deviation with adaptive windows
outlier_incremental.py — Detect spikes via abrupt changes between records
Advanced (machine learning, multi-method):
outlier_lof.py — Local Outlier Factor (density-based anomalies)
outlier_stepwise.py — Chain multiple methods for progressive filtering
Use Cases¶
Quick spike detection (Hampel filter):
import diive as dv
# Detection using the Median Absolute Deviation in a rolling window
detector = dv.outliers.Hampel(
series=df['NEE'],
n_sigma=5.5,
window_length=48 * 13, # record count (13 days of 30-min data), or a time span such as '13D'
separate_day_night=True,
lat=47.286417,
lon=7.733750,
utc_offset=1
)
detector.calc(repeat=False)
flag = detector.flag # 0=tested and accepted, 2=outlier, NaN=not testable (missing value)
cleaned = detector.filteredseries # outliers set to NaN
Statistical thresholding (Z-score):
from diive.preprocessing.outlier_detection import StepwiseOutlierDetection
# Global z-score
detector = StepwiseOutlierDetection(dfin=df, col='FCH4', site_lat=47.5, site_lon=8.4, utc_offset=1)
detector.flag_outliers_zscore_test(thres_zscore=4)
# Day/night separated z-score
detector.flag_outliers_zscore_test(
thres_zscore=4,
separate_day_night=True
)
Absolute physical limits:
import diive as dv
# Enforce known physical bounds
detector = dv.outliers.AbsoluteLimits(
series=df['RH'],
minval=0,
maxval=100
)
detector.calc()
flag = detector.flag # 0=tested and accepted, 2=outlier, NaN=not testable (missing value)
cleaned = detector.filteredseries
Multi-stage filtering (sequential chain):
from diive.preprocessing.outlier_detection import StepwiseOutlierDetection
# Progressive filtering: aggressive first, then refine
detector = StepwiseOutlierDetection(dfin=df, col='NEE', site_lat=46.8, site_lon=8.6, utc_offset=1)
detector.flag_outliers_hampel_test(n_sigma=5.5, separate_day_night=True)
detector.addflag()
# The global knob applies to both periods; override one with n_sd_daytime /
# n_sd_nighttime (and winsize_daytime / winsize_nighttime) if they should differ
detector.flag_outliers_localsd_test(n_sd=3.5, winsize=24, separate_day_night=True)
detector.addflag()
detector.flag_outliers_zscore_test(thres_zscore=4)
detector.addflag()
# View final cleaned series
cleaned = detector.series_hires_cleaned
Running Examples¶
# Beginner-friendly (simple, deterministic)
uv run python examples/preprocessing/outlier_detection/outlier_absolutelimits.py
uv run python examples/preprocessing/outlier_detection/outlier_manualremoval.py
uv run python examples/preprocessing/outlier_detection/outlier_trim.py
# Intermediate (statistical, adaptive)
uv run python examples/preprocessing/outlier_detection/outlier_hampel.py
uv run python examples/preprocessing/outlier_detection/outlier_zscore.py
uv run python examples/preprocessing/outlier_detection/outlier_localsd.py
uv run python examples/preprocessing/outlier_detection/outlier_incremental.py
# Advanced (machine learning, multi-method)
uv run python examples/preprocessing/outlier_detection/outlier_lof.py
uv run python examples/preprocessing/outlier_detection/outlier_stepwise.py
# Run all outlier detection examples
uv run python examples/run_all_examples.py
Choosing a Method¶
Situation |
Recommended Method |
Why |
|---|---|---|
Quick QA/QC |
Hampel |
Fast, robust to skew, works with non-normal distributions |
Known physical limits |
AbsoluteLimits |
Simple, deterministic, no parameters to tune |
Normal distribution |
zScore |
Works well when data is normally distributed |
Varying patterns |
LocalSD |
Adaptive to local variability |
Abrupt changes |
Incremental |
Detects step changes, not global outliers |
Unknown patterns |
LOF |
Density-based, finds anomalies without assumptions |
Multiple tests |
StepwiseOutlierDetection |
Combine methods, each filters progressively |