Anomaly forest
polars_ts.anomaly_forest
Isolation Forest adapter for time series anomaly detection. Closes #63.
isolation_forest_detect(df, feature_cols, id_col='unique_id', time_col='ds', contamination=0.05, global_model=True, seed=42, **kwargs)
Detect anomalies using Isolation Forest on engineered features.
Requires scikit-learn (pip install polars-timeseries[ml]).
Parameters
df
Input DataFrame with feature columns already computed
(e.g. via lag_features, rolling_features, calendar_features).
feature_cols
Column names to use as features for the Isolation Forest.
id_col
Column identifying each time series.
time_col
Column with timestamps.
contamination
Expected proportion of outliers.
global_model
If True, fit a single model on all series.
If False, fit a separate model per series.
seed
Random seed.
**kwargs
Additional arguments passed to IsolationForest.
Returns
pl.DataFrame
Original DataFrame with "anomaly_score" (float, lower = more anomalous)
and "is_anomaly" (boolean) columns appended.