Jibran Khan
EN

Forecasting air pollution every hour with machine learning, and keeping it running

By · · 7 min read
Contents

Tomorrow at 08:00, nitrogen dioxide (NO₂) on Jagtvej in Copenhagen will peak at around 30 µg/m³. A machine learning model made that forecast this afternoon, and it will make another one within the hour. Nobody pressed a button.

Air quality forecasts matter because they protect health. When people know that a polluted hour is coming, they can act on it. An older person can go for a walk at a cleaner time, the parent of a child with asthma can plan the day, and a city can aim warnings and interventions at the people most at risk. That is why I built this live forecast, end to end and on free infrastructure, and found out what it takes to keep it running every hour without me. Training the model turned out to be the small part.

In short

  • An hourly air pollution forecast for the next 24 hours at one busy Copenhagen street, live since October 2026.
  • A LightGBM model is 30% more accurate than persistence (assuming the latest value continues), using weather forecasts as input.
  • Most of the work went into data quality, scheduling, automatic retraining and monitoring, not the model itself.
  • Everything is open: live dashboard, code and data.
The live dashboard with tomorrow’s NO₂ peak and the forecast for each of the next 24 hours
The public dashboard updates every hour with a new 24-hour forecast.

Why forecast air pollution hour by hour?

NO₂ is one of the main air pollutants in cities, and road traffic is a large source of it. It irritates the airways and can make asthma worse. The WHO guideline for a 24-hour average is 25 µg/m³.

Levels change from hour to hour with traffic and weather. A calm, cold morning traps exhaust near the ground, and a windy afternoon clears it. That makes NO₂ a good target for short-term forecasting. There is a strong daily rhythm, and the weather explains much of the rest.

The data, and what was wrong with it

The forecast uses two public sources:

  • Hourly NO₂ at the Jagtvej roadside station, from the European Environment Agency through OpenAQ.
  • Weather from Open-Meteo, with temperature, humidity, wind speed and wind direction, both as measured history and as archived forecasts.

Before training anything, I recomputed simple statistics from the raw hourly data. That step found four problems:

  1. An API endpoint labelled its years wrongly. The yearly summary was shifted by one year compared with the hourly data.
  2. NO₂ before November 2019 is implausible. Monthly means of 0.5 to 5 µg/m³ at a busy roadside, then a clean jump to normal levels. That points to a change in how the data was processed, so training starts in November 2019. The raw data stays untouched.
  3. NO₂ is almost entirely missing from March to November 2022. It stays missing. Filling it with invented values would teach the model patterns that never happened.
  4. A promising traffic signal disappeared. Nitric oxide (NO) would be a strong feature, but the sensor stopped reporting in 2024. A feature the live model cannot get is a feature the model cannot use.

None of this shows up in a model’s accuracy score. It shows up when you look at the data.

How the model forecasts NO₂ 24 hours ahead

Each training example is one forecast, made at a given hour for a target hour 1 to 24 hours later. The model is LightGBM, a gradient boosting method that works well on tabular time series.

The inputs are what you would expect from the physics and the traffic:

  • recent NO₂ levels at the station
  • hour of day and day of week, in Copenhagen local time, because traffic follows local clocks through daylight saving changes
  • forecast temperature, humidity, wind speed and wind direction

The most important design rule is no peeking into the future. Measurements arrive about three hours late, so the model only uses values that would really have arrived by the time the forecast is made. Automated tests check this rule, and training and live scoring share one function to build features, so they can never drift apart.

How accurate is it?

All numbers are mean absolute error (MAE) in µg/m³, on a full test year (October 2025 to October 2026) that the model never saw during training. Lower is better.

MethodError (MAE)Better than persistence
Typical value for that hour and weekday8.58
Same hour last week8.83
Persistence (the latest value continues)8.02
LightGBM with measured weather4.9438%
LightGBM with forecast weather5.5930%

The honest number is the last one. The model with measured weather looks better, but no live forecast knows tomorrow’s real weather. Using archived weather forecasts shows what the service actually achieves, about 30% better than persistence overall, and 30 to 35% better at 6 to 24 hours ahead.

Keeping a machine learning forecast running every hour

This is where most of the effort went. Four automated jobs run the system:

JobWhenWhat it does
Hourly pipelineEvery hourFetches new measurements and weather, makes the next 24-hour forecast, rebuilds the dashboard
Weekly snapshotMonday nightSaves a frozen, versioned copy of all data
Weekly retrainingAfter each snapshotTrains a new model and promotes it only if it is clearly better
Daily monitoringEvery dayChecks that data is fresh and that live accuracy holds

A schedule that actually runs every hour

GitHub Actions can run jobs on a schedule, but scheduled runs are best effort. In the first two days, my “hourly” job ran only every three to six hours. The fix was an external scheduler that starts the job through GitHub’s API every hour, with GitHub’s own schedule as a backup. Every run is safe to repeat, so a double run does no harm.

Champion and challenger models

Every week a new model, the challenger, is trained on the latest data. It competes with the current model, the champion, on the same recent 60 days with the same weather forecasts. The challenger replaces the champion only if it is at least 2% better. Smaller differences are mostly noise.

This has already worked both ways. A model retrained in August 2026 was promoted, because NO₂ at the station keeps falling year on year and the new model had learned the lower levels. Another challenger, with data from a second station added, was rejected, because it was only 0.5% better. It was a sensible idea from the physics that did not earn its place.

Monitoring and data drift

A daily check fails loudly if measurements stop arriving or if the live error over the last week rises well above the tested error. A weekly report compares recent inputs with the same season in earlier years.

One lesson surprised me. The standard drift alarm (a Population Stability Index above 0.25) fires almost every week for weather, because weather comes in multi-day spells. Normal weeks reached values of 1.6 to 4.8. The report now flags a week only when it is more unusual than 95% of past weeks. Textbook thresholds need checking against the physics of your data.

What it cannot do yet

  • One station. The model learns this street. It would need retraining for any other place.
  • No uncertainty range. The dashboard shows the typical error, not a proper forecast interval.
  • No forecast for the current hour. That would need its own model and evaluation.

These are the next things I would work on.

Frequently asked questions

Can machine learning forecast air pollution?

Yes, for short horizons it works well. At this station, a LightGBM model using weather forecasts is about 30% more accurate than assuming the latest value continues. The key inputs are recent measurements, time of day and weather.

What is MLOps in air quality forecasting?

MLOps is the work that keeps a model useful after training. It covers automated data collection, versioned data, scheduled retraining, model comparison, and monitoring of data and accuracy. For a forecast that runs every hour, it is most of the work.

Which model works best for hourly NO₂ forecasts?

Gradient boosting models such as LightGBM are a strong choice for this kind of tabular time series. They are fast to train, handle missing values, and capture the daily and weekly rhythms of traffic together with the weather.

Does it cost anything to run?

No. Everything runs on free tiers: GitHub Actions and GitHub Pages, Supabase for the database, Hugging Face for the versioned data, and DagsHub for experiment tracking.

Try it and look inside

Topics: air quality forecasting machine learning MLOps time series

Spotted an error, or see it differently? Leave a comment below, or write to me privately at email.

Comments