Kaggle api streaming inference

From wenmin-wu/ds-skills · View interactive version

Kaggle api streaming inference is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Predict day-by-day via Kaggle's iter_test API while maintaining a rolling history buffer for computing lag features online

How to use this skill

Skill file

Kaggle API Streaming Inference

Overview

Kaggle time-series competitions often use a streaming API where test data arrives one day at a time. The model must predict using only data available up to that point. This requires maintaining a rolling history buffer, computing lag features on the fly, and appending each day's predictions back to the buffer for use as future lag inputs.

Quick Start

import pandas as pd
from datetime import timedelta

history = train_df[["entity_id", "date"] + TARGETS].copy()

env = competition.make_env()
for test_df, sample_sub in env.iter_test():
    eval_date = pd.to_datetime(test_df["date"].iloc[0])

    # Compute lag features from history
    lag_features = []
    for lag in range(1, 21):
        lag_date = eval_date - timedelta(days=lag)
        lag_vals = history[history["date"] == lag_date][["entity_id"] + TARGETS]
        lag_vals = lag_vals.rename(columns={t: f"{t}_{lag}" for t in TARGETS})
        lag_features.append(lag_vals)

    features = test_df[["entity_id"]].copy()
    for lf in lag_features:
        features = features.merge(lf, on="entity_id", how="left")
    features = features.fillna(0)

    preds = model.predict(features[lag_cols])
    sample_sub[TARGETS] = preds

    # Update history with today's predictions
    new_row = test_df[["entity_id", "date"]].copy()
    new_row[TARGETS] = preds
    history = pd.concat([history, new_row], ignore_index=True)

    env.predict(sample_sub)

Workflow

  1. Initialize history buffer with all available training data
  2. For each test day received from iter_test():

a. Extract the evaluation date

b. Look up lag values from the history buffer

c. Construct the feature vector matching the training schema

d. Predict and submit

e. Append predictions to history for future lag computation

  1. Keep only the last N days in the buffer to limit memory usage

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.