Skip to content
Back to skills

Co Purchase Item Pairing

ASecurity

Recommends items frequently purchased together with a customer's recent items using pre-computed pair dictionaries.

  • 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 co-purchase-item-pairing --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Co Purchase Item Pairing?

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

Security grade badge for Co Purchase Item Pairing
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-co-purchase-item-pairing/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-co-purchase-item-pairing)

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: tabular-co-purchase-item-pairing
description: >
  Recommends items frequently purchased together with a customer's recent items using pre-computed pair dictionaries.
---
# 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

```python
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

- **Time window**: 1-7 days captures session-like co-purchases
- **Pair direction**: Bidirectional (A→B and B→A) or directional (sequential)
- **Fallback**: Use as supplement to main model, not standalone
- **Pre-compute**: Store pairs as dict/numpy for fast lookup at inference

## References

- H&M Personalized Fashion Recommendations (Kaggle)
- Source: [recommend-items-purchased-together-0-021](https://www.kaggle.com/code/cdeotte/recommend-items-purchased-together-0-021)

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.