← Back to case study

PROJECT 080 · PYTHON SOURCE

Cross-category spending relationships

Study-specific code. Shared modules, dependency versions, and reproduction instructions are included in all project files.

Download Python file ↓
from pathlib import Path
import sys
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT))
import numpy as np
import pandas as pd
from scipy.stats import spearmanr
from sklearn.decomposition import PCA
from sklearn.ensemble import IsolationForest
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix, precision_score, recall_score
from portfolio.data import load
from portfolio.constants import *
from portfolio.methods import result, sql, rates, associations, distribution, regression, classification, cluster
from portfolio.engine import execute

META = {'id': 80, 'dataset': 'wholesale', 'title': 'Cross-category spending relationships', 'question': 'Which spending categories show the strongest customer-level rank associations?', 'method': 'Pairwise Spearman correlations across all six categories.', 'action': 'Identify bundle hypotheses and test incremental response instead of assuming co-spend implies bundle demand.', 'limitations': 'Annual customer spending uses source monetary units, not an assumed currency. There are no margins, transactions or dates. Customer segments are descriptive and do not establish promotion response. '}

def analyze():
    df = load('wholesale').copy()
    rows=[]
    for i,a in enumerate(SPEND):
        for b in SPEND[i+1:]:
            rows.append({'pair':a+' / '+b,'spearman_rho':df[[a,b]].corr(method='spearman').iloc[0,1]})
    t=pd.DataFrame(rows).sort_values('spearman_rho',ascending=False)
    out=result(t,'pair','spearman_rho','Spearman correlation','Customer scale and channel can induce correlations across categories.')
    return out

if __name__ == "__main__":
    execute(Path(__file__).parent, META, analyze)