100 beginner machine learning projects with runnable Python code, tests and honest case studies. By Networkers Home, Bangalore.
This project teaches a statistics-first approach to flagging anomalous sensor readings. Using a synthetic time series with a seasonal baseline and injected incidents, the code computes two kinds of thresholds from an initial baseline window: a classic z-score threshold (mean ± k·std) and a robust median/MAD rule. The held-out portion of the series is evaluated using these statistical cutoffs, and simple alert validation is performed against the known incidents. The focus is on counting true/false alerts and measuring precision and recall so students can appreciate the burden of false alarms and the need for human review.
The data are synthetic and produced inside the code. A sinusoidal seasonal signal plus Gaussian noise models a permitted sensor series. Discrete anomaly spikes and sustained deviations are injected at known timestamps to simulate incidents; these provide ground truth for validation.
Run the module directly: python project.py. That prints a JSON dictionary of metrics for the held-out evaluation. You can also import run from project and call run(seed) from other code.
Run pytest in the project directory: pytest -q
Measured by running python project.py (seed 42) on 2026-10-23; the tests in test_project.py assert the floor.
| Metric | Value |
|---|---|
| precision | 1 |
| recall | 0.0370 |
| tp | 1 |
| fp | 0 |
| fn | 26 |
| baseline_mean | -0.0326 |
| baseline_std | 3.5269 |
| baseline_median | -0.0781 |
| baseline_mad_scaled | 4.8984 |
| upper_z | 8.7845 |
| lower_z | -8.8498 |
| upper_mad | 14.6170 |
| lower_mad | -14.7732 |
| held_out_size | 600 |
Read the
precisionvalue with care: the data here is small and synthetic with a planted, learnable structure, so a near-perfect score shows the pipeline works, not that the method would reach this on real data. See Limitations.
This is a compact educational example. The synthetic series approximates a seasonal sensor but is much simpler than real-world multi-sensor telemetry. The rules here are univariate and do not model correlated signals. Thresholds and window choices are fixed for clarity; a production deployment would need continuous recalibration, drift handling, and a logged human-review workflow.
Suggested extensions include: (1) compare different baseline window sizes and quantify how quickly the detector adapts, (2) incorporate rolling MAD rather than a single baseline MAD, (3) add a simple human-review log (a labelled buffer) to simulate escalation decisions, and (4) explore small supervised classifiers or time-series models only after careful labelling and review.
Full case study, build steps and evaluation checks: Anomaly detection using statistics on networkershome.com
Built by Networkers Home · Learn the skills in the AI Engineer Course · Part of 100 AI projects with code