Co purchase item pairing

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

Co purchase item pairing is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Recommends items frequently purchased together with a customer's recent items using pre-computed pair dictionaries.

How to use this skill

Skill file

Co-Purchase Item Pairing

Overview

For each item, pre-compute the most frequently co-purchased item (bought by the same customer in the same session or time window). At inference, map each of a customer's recent purchases to its paired item and add those as recommendations. This captures "bought together" patterns that collaborative filtering may miss.

Quick Start

import pandas as pd
from collections import Counter

def build_pair_dict(transactions, time_window_days=7):
    """Find most common co-purchase for each item."""
    df = transactions.sort_values(["customer_id", "t_dat"])
    df["next_item"] = df.groupby("customer_id")["article_id"].shift(-1)
    df["day_diff"] = df.groupby("customer_id")["t_dat"].diff(-1).dt.days.abs()
    pairs = df[df["day_diff"] <= time_window_days]

    pair_counts = pairs.groupby("article_id")["next_item"].apply(
        lambda x: Counter(x).most_common(1)[0][0]
    )
    return pair_counts.to_dict()

pairs = build_pair_dict(transactions)
# At inference: map customer's recent items to co-purchased items
recs = [pairs.get(item) for item in customer_history if item in pairs]

Workflow

  1. Identify co-purchase pairs within a time window per customer
  2. For each item, store the most common co-purchased partner
  3. At inference, map customer's recent items through the pair dict
  4. Deduplicate and append to recommendation list

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.