Urban Analytics & Transportation · Part 1 of 7

The Counts and Their Holes

One row per camera and minute, a day that follows the prayers, and four kinds of holes to fill.

The table

The data are two files from the Madinah Ethical AI Competition on Kaggle. Thirteen cameras in Madinah each report, once a minute, how many people they see. Stacked, the files give one row per camera and minute:

import os

import numpy as np
import pandas as pd

import crowd

d = crowd.load()                     # train.csv and test.csv: one row per camera and minute
show = ["recording", "day", "weekday", "unit", "minute_of_day", "people", "kind"]
print(pd.concat([d[d.people.notna()].head(3), d[d.people.isna()].head(3)])[show].to_string())
           recording         day weekday   unit  minute_of_day  people      kind
0      20260904_1105  2026-09-04     Fri  cam_B            676  13.103       NaN
1      20260904_1105  2026-09-04     Fri  cam_B            677  17.157       NaN
2      20260904_1105  2026-09-04     Fri  cam_B            678  23.348       NaN
13447  20260918_1100  2026-09-18     Fri  cam_I            844     NaN  forecast
13448  20260918_1100  2026-09-18     Fri  cam_I            845     NaN  forecast
13449  20260918_1100  2026-09-18     Fri  cam_I            846     NaN  forecast
columnmeaning
recordingone stretch of filming, named by its date and starting time
day, weekdaythe date; every recording falls on a Friday or a Saturday
unitthe camera, cam_A to cam_M
minute_of_dayminutes after midnight: 676 is 11:16
peoplethe count; NaN where the value is a hole
kindfor a hole, which kind of hole it is

crowd.load() only reads the two files and stacks them. The file crowd.py holds this and a few other helpers that are about the data and not about the model.

Six days of filming

hhmm = lambda m: f"{m // 60:02d}:{m % 60:02d}"
rec = d.groupby(["recording", "weekday"]).agg(
    start=("minute_of_day", lambda m: hhmm(m.min())),
    end=("minute_of_day", lambda m: hhmm(m.max())),
    cameras=("unit", "nunique"),
    counts=("people", "count"),
    holes=("people", lambda p: p.isna().sum()))
print(rec)
                       start    end  cameras  counts  holes
recording     weekday                                      
20260904_1105 Fri      11:05  14:30        7    1111      0
20260905_0426 Sat      04:26  08:00        7    1032      0
20260905_1124 Sat      11:24  13:37        7     824      0
20260905_1900 Sat      19:00  21:44        7     955      0
20260911_1100 Fri      11:00  21:14        8    4726      0
20260912_1100 Sat      11:00  21:14        8    4795      0
20260918_1100 Fri      11:00  21:14       13    1927   2334
20260919_1100 Sat      11:00  21:14       13    2227   2123

What a day looks like

Counts of camera A and camera G through Saturday 12 September
Two cameras on Saturday 12 September. The dotted lines are the calls to prayer. Both cameras follow the same rhythm, at very different sizes.

Two things in this picture shape the whole model.

The day follows the prayers. The call to prayer is the adhan; four of the five daily adhans fall inside the recording: dhuhr (noon), asr (afternoon), maghrib (sunset) and isha (night). The count falls in the quarter of an hour after each adhan, when people go in to pray, and surges about half an hour after it, when they come out.

The cameras differ in size, not in kind.

full = d[d.day.isin(["2026-09-11", "2026-09-12"])]            # the two complete days
size = full.groupby("unit").people.agg(["min", "median", "max"]).round(0).astype(int)
print(size.T)
unit    cam_A  cam_B  cam_C  cam_D  cam_E  cam_F  cam_G  cam_H
min        44      3      3      3     56     20      1      3
median    189     23     20     28    235    126      7     14
max       704     77    125     82    999    385     33     59

Camera E sees 235 people at its median and camera G sees 7, a factor of more than 30. A surge that doubles the count adds 200 people to one camera and 7 to the other. On the log scale the two surges are the same: doubling adds log 2 = 0.69 to both. So the model describes the logarithm of the count. There, effects that multiply the count become terms that add up, and one term can serve cameras of every size.

Four kinds of holes

On 18 and 19 September the organisers removed values in four ways:

print(d[d.people.isna()].groupby(["day", "kind"]).size().unstack())
kind        forecast  hidden  hidden-future  missing
day                                                 
2026-09-18      1230     564            322      218
2026-09-19      1430     285            162      246
kindwhat is missingwhat is still known
forecastthe 15 minutes after a cutoff, for a camera that is onevery camera up to the cutoff
hiddenevery row of one camera on that daythe other cameras on that day, and this camera on other days
hidden-futurethe 15 minutes after a cutoff, for a hidden camerathe other cameras up to the cutoff, and this camera on other days
missinga gap of a few minutesthe camera before and after the gap, and the other cameras during it

Each of the two days has 11 cutoffs. After a cutoff the table is blank for 30 minutes for every camera; the first 15 of them are the holes to fill.

Which minutes of each camera are counts and which are holes, on Saturday 19 September as released and on Saturday 12 September with the holes made here
Every minute of every camera, as a count (teal) or a hole to fill (amber); white means there is no row. Top: Saturday 19 September as released. Camera D is hidden for the whole day, each cutoff is followed by a block of holes, and short gaps are scattered between them. Bottom: the same kinds of holes, made here on Saturday 12 September.

Holes with known truth

The values behind the holes of 18 and 19 September are not public. A model that fills them cannot be checked there. So we make the same holes one week earlier, where every value is known:

Why 30 minutes later. A forecast can only replay what the model has seen on other days at the same minutes. The two later days are blank for 30 minutes after each of their cutoffs. With our cutoffs at those same minutes, every long day would be blank exactly where the model has to forecast. Moved by 30 minutes, each minute to forecast is one that the later day shows. That matches real use, where the earlier days are complete.

d = d[d.recording != "20260905_0426"]            # the fragment at dawn is left out
d, plan = crowd.rehearsal(d)                     # adds obs, role and rnow
print(crowd.holes(d).groupby(["day", "kind"]).size().unstack())
print()
print("rows", len(d), "| the model may use", int(d.obs.sum()), "| to predict", int((~d.obs).sum()))
os.makedirs("work", exist_ok=True)               # the parts save their files here
d.to_pickle("work/table.pkl")
kind        forecast  hidden  hidden-future  missing
day                                                 
2026-09-11       855     622            298      164
2026-09-12      1017     314            148      187

rows 21022 | the model may use 10637 | to predict 10385

crowd.rehearsal() adds three columns: obs says whether the model may use the value, role is the kind of hole we made, and rnow is its cutoff. From here on the model sees a count only where obs is true. The values behind our holes are kept aside and used in Parts 6 and 7 to check the predictions.

The fragment at dawn on 5 September is left out: it is far from the hours that have holes.

The code of this part, with the output it printed: 01_data.ipynb. It reads crowd.py and the files the earlier parts saved in work/.

Next: Part 2: How Big, How Busy →

Part 2 starts the model with the simplest question: how big is each camera, and how busy is each day?