Time Series Anomaly Detection in Python
Detect anomalies in a time series using rolling z-score and Isolation Forest, then visualize flagged points.
What this AI workflow does
This AI Data Analyst workflow loads the NYC Taxi Demand time series from a URL and plots demand over time. It detects anomalies using a rolling mean with a 3-sigma (rolling z-score) rule and visualizes the flagged points on the series. It then applies Isolation Forest for anomaly detection and compares which timestamps are flagged by each method.
Who this example is for
This is for data analysts and data scientists who need a reproducible notebook pattern for time series anomaly detection. It helps users compare a simple statistical thresholding approach with a machine-learning method on the same dataset.
Expected analysis outcomes
These are the results the AI workflow is expected to generate.
- Loaded and parsed NYC taxi demand time series from a remote CSV
- Line chart of taxi demand over time
- Rolling z-score (3-sigma) anomalies highlighted on the time series
- Isolation Forest anomalies highlighted and compared against the z-score method
Tools and libraries used
Main Python packages and tooling used to run this AI data analysis task.
- pandas
- numpy
- matplotlib
- seaborn
- scikit-learn
Prompt sequence
This is the exact list of prompts used in this workflow. The same prompt sequence is sent to each model so outputs and scores can be compared fairly.
- 1load NYC taxi demand data from https://raw.githubusercontent.com/pplonski/datasets-for-start/refs/heads/master/nyc-taxi-demand/nyc_taxi.csv and plot the time series
- 2detect anomalies using rolling mean and 3-sigma rule, plot flagged points
- 3use Isolation Forest to detect anomalies and compare with z-score method
Model Comparison
We compared several LLM models on the same analysis task. The table below shows their scores side by side. You can also open each model run to check the full conversation and notebook results.
| Model Source | Score | Task | Execution | Output | Reasoning | Reliability | Run |
|---|---|---|---|---|---|---|---|
| glm-5.1 | 10/10 | 2/2 | 2/2 | 3/3 | 2/2 | 1/1 | Open glm-5.1 conversation |
| qwen3.5:397b | 10/10 | 2/2 | 2/2 | 3/3 | 2/2 | 1/1 | Open qwen3.5:397b conversation |
| gpt-5.4 | 8/10 | 2/2 | 2/2 | 2/3 | 1/2 | 1/1 | Open gpt-5.4 conversation |
| gpt-oss:120b | 8/10 | 2/2 | 2/2 | 2/3 | 1/2 | 1/1 | Open gpt-oss:120b conversation |
| qwen3-coder-next | 7/10 | 2/2 | 2/2 | 2/3 | 1/2 | 0/1 | Open qwen3-coder-next conversation |
| gemma4:31b | 6/10 | 2/2 | 1/2 | 2/3 | 1/2 | 0/1 | Open gemma4:31b conversation |
How scoring works
Each run is graded on five dimensions:
- Task Completion (0-2): how fully the workflow answered the requested analysis task and completed required steps.
- Execution Correctness (0-2): whether generated code is correct, consistent, and likely runnable without major fixes.
- Output Quality (0-3): how well tables, charts, and results match the expected analytical outcomes.
- Reasoning Quality (0-2): clarity and correctness of interpretation, explanation, and conclusions.
- Reliability (0-1): robustness of the run, including low hallucination risk and stable behavior.
The final score is the sum of these dimensions, so total score = 0-10. Higher values indicate more complete, correct, and reliable notebook results for this workflow.
AI Analysis with gemma4:31b
This run uses AI with a step-by-step list of prompts to perform the analysis task Time Series Anomaly Detection in Python. Below, you can review the full conversation generated with gemma4:31b. After the conversation, we show detailed score cards for this run.
Score cards for gemma4:31b for Time Series Anomaly Detection in Python
Total score: 6/10
Task Completion: 2/2
ExcellentAll major requested steps were attempted: data loading, time-series plotting, rolling 3-sigma anomaly detection, and Isolation Forest comparison.
Execution Correctness: 1/2
Needs workThe code is mostly runnable, but the rolling anomaly step produced zero anomalies and the notebook content suggests some inconsistency in the workflow narrative. The Isolation Forest step is syntactically plausible.
Output Quality: 2/3
GoodThe notebook includes the required plots and comparison outputs, but the anomaly detection results are weak: the rolling method flags none, so the highlighted anomaly plot does not meaningfully satisfy the expected outcome.
Reasoning Quality: 1/2
Needs workThe reasoning is partially correct, but it mixes incompatible anomaly approaches and uses a global z-score instead of the requested rolling z-score for the comparison. The explanation also overstates what was achieved.
Reliability: 0/1
Needs workThe workflow is fragile and inconsistent with the prompt, especially because the requested rolling z-score comparison is not properly implemented and the anomaly results are not robust.
Try MLJAR Studio
Run the same type of AI-powered data analysis on your own datasets with conversational notebooks in MLJAR Studio.
Explore More AI Analysis Examples
Discover additional workflows across categories. Each example includes prompts, conversation outputs, and model-level scoring so you can compare approaches and results.