Skip to content
Back to skills

Type Weighted Covisitation Matrix

ASecurity

Build item co-visitation matrix from session pairs within a time window, weighting by interaction type (click/cart/order) via GPU self-join

  • 61 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 12, 2026
developmentpythonapi

Works with

  • cli
  • api

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill type-weighted-covisitation-matrix --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Type Weighted Covisitation Matrix?

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

Security grade badge for Type Weighted Covisitation Matrix
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-type-weighted-covisitation-matrix/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-type-weighted-covisitation-matrix)

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-type-weighted-covisitation-matrix
description: Build item co-visitation matrix from session pairs within a time window, weighting by interaction type (click/cart/order) via GPU self-join
domain: tabular
---

# Type-Weighted Co-visitation Matrix

## Overview

For session-based recommendation, build a co-visitation matrix by self-joining session events within a time window. Weight each co-occurrence by interaction type (e.g. clicks=1, carts=6, orders=3) so high-intent signals dominate. Use RAPIDS cuDF for GPU-accelerated computation on large datasets. Extract top-K co-visited items per anchor for candidate generation.

## Quick Start

```python
import cudf

def build_covisitation_matrix(df, type_weight={0:1, 1:6, 2:3},
                               window_hours=24, top_k=20):
    """Build type-weighted co-visitation from session events.
    
    Args:
        df: DataFrame with columns [session, aid, ts, type]
        type_weight: {event_type: weight} mapping
        window_hours: time window for co-occurrence (hours)
        top_k: top co-visited items to keep per anchor
    """
    window_sec = window_hours * 3600
    df = df.merge(df, on='session')
    df = df.loc[
        ((df.ts_x - df.ts_y).abs() < window_sec) &
        (df.aid_x != df.aid_y)
    ]
    df = df[['session','aid_x','aid_y','type_y']].drop_duplicates(
        ['session','aid_x','aid_y'])
    df['wgt'] = df.type_y.map(type_weight).astype('float32')
    
    # Aggregate and keep top-K per anchor
    pairs = df.groupby(['aid_x','aid_y']).wgt.sum().reset_index()
    pairs = pairs.sort_values(['aid_x','wgt'], ascending=[True, False])
    pairs['n'] = pairs.groupby('aid_x').aid_y.cumcount()
    return pairs.loc[pairs.n < top_k].drop('n', axis=1)
```

## Key Decisions

- **Type weights**: carts >> orders > clicks reflects purchase intent; tune on validation
- **Buy2buy variant**: filter to carts/orders only with a 14-day window for longer-horizon purchase affinity
- **Time-weighted variant**: scale weights linearly by recency (1-4x) to emphasize recent co-visits
- **GPU self-join**: cuDF handles billion-row self-joins; chunk by time period if OOM
- **Top-K per anchor**: 20-40 is typical; higher for cold-start items

## References

- Source: [candidate-rerank-model-lb-0-575](https://www.kaggle.com/code/cdeotte/candidate-rerank-model-lb-0-575)
- Competition: OTTO - Multi-Objective Recommender System

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…