Prepare the series
Assemble a chronological series with stable timestamps, fill or mark missing values and inspect for trend and seasonality.
Detect anomalies in time series. Inspect unexpected deviations.
Anomaly detection in time series aims to highlight timestamps where behaviour deviates from expected patterns. Good practice includes building stable timestamps, creating lag and rolling features, using walk-forward evaluation to avoid leakage, and defining clear detection rules for residuals or prediction intervals. Typical failure modes are non-stationary seasonality, concept drift, clustered anomalies and missing data.
Learning concept · Not a validated service
Anomaly detection in time series aims to highlight timestamps where behaviour deviates from expected patterns. Good practice includes building stable timestamps, creating lag and rolling features, using walk-forward evaluation to avoid leakage, and defining clear detection rules for residuals or prediction intervals. Typical failure modes are non-stationary seasonality, concept drift, clustered anomalies and missing data.
A proposed monitoring extension: run a forecasting baseline on sensor or operational streams, then flag timestamps where residuals exceed a domain-calibrated threshold; always route alerts to a human reviewer for verification.
Assemble a chronological series with stable timestamps, fill or mark missing values and inspect for trend and seasonality.
Add lags, rolling statistics and calendar indicators so the model can learn short-term and periodic structure.
Fit a regression model (here a RandomForest) on earlier data and predict a hold-out window using only past features.
Convert forecasts to residuals, choose a threshold or prediction-interval method, then measure detection precision, recall and false-alarm rate.
The supplied starter creates a synthetic daily series with trend, sinusoidal seasonality and Gaussian noise, computes lag (1,7,14) and a 7-day rolling mean, trains a RandomForestRegressor to forecast the final 30 days, prints mean absolute error and shows a few actual vs forecast rows. It does not implement anomaly detection logic (no residual-thresholding, scoring of anomalies, or alerting).
for lag in [1, 7, 14]:
series[f"lag_{lag}"] = series["value"].shift(lag)
series["rolling_7"] = series["value"].shift(1).rolling(7).mean()
series = series.dropna()
feature_cols = ["lag_1", "lag_7", "lag_14", "rolling_7"]
train = series.iloc[:-30]
test = series.iloc[-30:]
model = RandomForestRegressor(n_estimators=300, random_state=42)
model.fit(train[feature_cols], train["value"])
forecast = model.predict(test[feature_cols])The code fits a forecasting baseline but does not produce anomaly flags: it lacks residual thresholding or statistical detection rules, no evaluation of detection precision/recall, and no walk-forward scoring for backtesting anomalies.
Proposed extension: use the baseline forecasts to compute residuals, set an empirical threshold (for example 3×MAD or a percentile), generate alerts for timestamps exceeding it, and provide a review interface where operators confirm or dismiss flagged anomalies.
This is a potential application pathway, not a delivered product. The supplied starter does not implement this service or validate it with real users.
Networkers Home runs this topic inside its AI Engineer Course, with placement support and hands-on labs.