Skip to content
Back to skills

Multi Lag Target Features

ASecurity

Generate lag features for multiple targets over N days by shifting evaluation dates and self-joining per entity, creating a wide feature matrix of past target values

  • 61 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added September 12, 2026
datapython

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill multi-lag-target-features --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Multi Lag Target Features?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Multi Lag Target Features
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-multi-lag-target-features/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-multi-lag-target-features)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: timeseries-multi-lag-target-features
description: Generate lag features for multiple targets over N days by shifting evaluation dates and self-joining per entity, creating a wide feature matrix of past target values
---

# Multi-Lag Target Features

## Overview

For entity-level time-series forecasting with multiple targets, lagging each target by 1..N days and pivoting into separate columns creates a dense feature matrix capturing recent history. The self-join approach shifts the date column by the lag amount and merges back on (entity_id, date), producing columns like `target1_1`, `target1_2`, ..., `target4_20` — 80 features for 4 targets x 20 lags.

## Quick Start

```python
from datetime import timedelta

TARGETS = ["target1", "target2", "target3", "target4"]
LAGS = list(range(1, 21))

def add_lag(df, lag):
    lagged = df[["entity_id", "date"] + TARGETS].copy()
    lagged["date"] = lagged["date"] + timedelta(days=lag)
    df = df.merge(lagged, on=["entity_id", "date"], suffixes=("", f"_{lag}"), how="left")
    return df

for lag in LAGS:
    df = add_lag(df, lag)

lag_cols = [f"{t}_{l}" for l in reversed(LAGS) for t in TARGETS]
```

## Workflow

1. Start with a long-format DataFrame: one row per (entity_id, date)
2. For each lag value, copy the target columns, shift dates forward by lag days
3. Merge back on (entity_id, date) with a lag suffix
4. Missing lags (start of series or gaps) become NaN — fill with 0 or entity median
5. Use the lag columns as features for any model (GBDT, ANN, linear)

## Key Decisions

- **Lag range**: 1-20 for daily data captures ~3 weeks of history; extend to 30+ for weekly patterns
- **Offset**: add an offset (e.g., 45 days) if targets aren't available until a delay after the evaluation date
- **Fill strategy**: fillna(0) is simple; entity-level median preserves scale differences
- **Memory**: for large datasets, compute lags in chunks or use `shift()` within groups instead of merge
- **At inference**: maintain a rolling history buffer and pivot the last N days into lag columns

## References

- [[Fork of] LightGBM + CatBoost + ANN 2505f2](https://www.kaggle.com/code/somayyehgholami/fork-of-lightgbm-catboost-ann-2505f2)
- [MLB: ANN with Lags TF Keras](https://www.kaggle.com/code/ulrich07/mlb-ann-with-lags-tf-keras)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.