aiml

Lesson 1 of 21Module 1

Use only data available when the prediction is made

Separate event time, arrival time and label readiness so a historical return prediction uses only available inputs and finalized outcomes.

Prerequisites

Python functions, dictionaries, UTC timestamps and basic supervised learning. Start with the course application brief.

Preparation and term definitions

By the end, you can

  • Distinguish event time, arrival time and label readiness.
  • Exclude late events from a historical feature without moving its prediction cutoff.
  • Explain why a pending outcome cannot become a negative training label.
Runtime
Python 3.12+; local CPU
Dependencies
Python standard library and SQLite

Lab code, tests and fixtures (ZIP, 1.6 MB)Quiz check your understanding

Progress is saved in this browser.

Separate the time an event happened, the time it arrived and the time a return label became final.

Problem

When you need this. At noon, the model can use events the application has already received. An event that happened at 11:50 but arrived at 12:05 was unavailable to the noon prediction. A later data export can contain both events, so filtering only by event time would give a historical model information it could not have used.

The same issue appears on the outcome side. A user’s return target covers the next 24 hours. In this fixture, the outcome collector takes another two hours to finalize it. The workshop follows one observation from the shared dataset through both availability decisions.

Concepts

Event time describes when the activity happened. Arrival time describes when the application received it. The feature counts events from the 24 hours before prediction time. An event counts only when it both happened and arrived before prediction time. An event exactly at the prediction cutoff belongs to the next window.

A label describes the future outcome used for learning. It is not an input to the current prediction. The fixture assumes a complete outcome collector that publishes a finalized return label after 26 hours: a 24-hour outcome window and two hours of reporting delay.

The feature-event log is intentionally partial. Its events build the input. Separate label records hold the finalized outcomes. Counting later rows in that partial log cannot replace the outcome collector.

The retrospective count ignores arrival time on purpose. Use it to see why the counts differ, not as a training feature.

Architecture

Event recordsevent + arrival timeApply cutoffsboth precede predictionKnown event count2 for this observationLate arrivalexcluded from this featureOutcome labelfinal after 26 hoursCheck readinesswait for finalized labelTraining exampleavailable feature + labelEvent recordsevent + arrival timeApply cutoffsboth before predictionKnown event count2 for this observationOutcome labelfinal after 26 hoursCheck readinesswait for finalized labelTraining examplefeature + finalized label
Features use only earlier arrivals. The outcome becomes training data after its separate finalization delay.

Working code

Download the shared application and tests, unzip it and enter mlops-return-workflow/. Keep the same directory throughout the course. Create the environment once. This workshop uses the standard library.

Create the local environmentTerminal · bash
python3.12 -m venv .venv
source .venv/bin/activate

On PowerShell, activate with .venv\Scripts\Activate.ps1.

The opening commands read scenario.json; no fitted model is needed.

Inspect one prediction and its delayed outcome

The command runs the existing assembler twice for the same observation: 25 hours after prediction time and at label readiness. It also compares the eligible feature events with a retrospective count that incorrectly includes a late arrival.

Source excerpt: availability_reportSource · pythonDownload
def availability_report(source):
    fixture = w.strict_json(source)
    chosen = next(o for o in fixture['observations'] if o['id']=='user-00-day-03')
    label = next(l for l in fixture['labels'] if l['id']==chosen['id'])
    at = w.availability.instant(chosen['predict_at'])
    early = w.availability.assemble([chosen], fixture['events'], [label], (at+timedelta(hours=25)).isoformat())
    ready = w.availability.assemble([chosen], fixture['events'], [label], label['ready_at'])
    known = w.availability.feature_events(chosen, fixture['events'])
    retrospective = w.availability.feature_events(chosen, fixture['events'], require_arrival=False)
    return {'observation':chosen['id'], 'predict_at':chosen['predict_at'],
            'outcome_end':(at+timedelta(hours=24)).isoformat(), 'label_ready_at':label['ready_at'],
            'available_event_count':len(known), 'retrospective_event_count':len(retrospective),
            'late_events_excluded':sorted(set(retrospective)-set(known)),
            'at_25_hours':early['exclusion_counts'], 'at_label_readiness':len(ready['rows'])}
Inspect one prediction and its delayed outcomeTerminal · bash
python adapt.py availability
Actual local application outputRecorded output · jsonDownload
{
  "observation": "user-00-day-03",
  "predict_at": "2026-01-03T12:00:00Z",
  "outcome_end": "2026-01-04T12:00:00+00:00",
  "label_ready_at": "2026-01-04T14:00:00Z",
  "available_event_count": 2,
  "retrospective_event_count": 3,
  "late_events_excluded": [
    "user-00-day-03-late"
  ],
  "at_25_hours": {
    "label_pending": 1
  },
  "at_label_readiness": 1
}

For user-00-day-03, two events were available at prediction time. A retrospective event-time-only count returns three. The extra event, user-00-day-03-late, happened at 11:50 and arrived at 12:05. At 25 hours the label is still pending; at its declared 26-hour readiness time the assembler admits the example for training.

The output above is from a saved local run in recorded-adaptations/; your commands use work/. Read the actual command records. Use a fresh work directory for another attempt.

Failure cases

Run test_arrival_and_label_readiness_have_separate_effects. It checks that one late event adds one to the retrospective count, that the label is still pending at 25 hours and that the example is admitted at label readiness.

The reused validator rejects an event that arrives before it happened and a label finalized before its outcome window ends. It cannot tell whether a real external collector is complete. Only the source system and its operating contract can promise that.

Run the application failure checksTerminal · bash
python -m unittest -v test_workflow.py test_foundations.py

Running and scaling the workshop

The command reads the bundled fictional fixture and applies the availability rules locally. It does not wait 26 real hours or contact an event service.

When this becomes an ingestion pipeline, store the original arrival timestamp and the outcome collector’s finalization marker. Do not use a backfill’s publication time as the historical arrival time.

Key takeaways

  • A historical event is usable only if it was already available to the prediction.
  • A finalized label becomes training data after its outcome and reporting windows.
  • A retrospective count can explain a difference without becoming the production feature.

Best practices

Apply the same cutoff rule during training and prediction.

Keep label availability separate from feature freshness. Fresh features do not make an unfinished outcome label valid.

Exclude an example whose label is still pending; do not fill it with 0. Assemble the cohort again after the label’s ready_at time.

Keep the reason for each excluded example. The assembler reports label_pending, label_missing or prediction_in_future, so you can explain why a training row is absent.

Check your understanding

Why does the assembler exclude the example at 25 hours when the 24-hour return window has ended?

Show the explanation

The fixture’s outcome collector needs another two hours to finalize the label. Ending the outcome window and receiving a complete outcome are separate events.

Why is the return label still excluded 25 hours after prediction?

Choose one answer, then check your reasoning.

Read the explanation

The outcome covers 24 hours, and this fixture allows another two hours for reporting. At 25 hours the finalized label is unavailable.

References

Written by Arjun Prabhulal.

Module 1

Define the application