← Back to case study

PROJECT 077 · PYTHON SOURCE

Wholesale channel assortment

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': 77, 'dataset': 'wholesale', 'title': 'Wholesale channel assortment', 'question': 'How does the median spending mix differ between recorded sales channels?', 'method': 'Channel-level medians across six spending categories.', 'action': 'Use channel profiles to frame an assortment discussion, then validate with transaction and margin data.', '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()
    t=df.groupby('channel')[SPEND].median().reset_index().melt(id_vars='channel',var_name='category',value_name='median_spend')
    t['channel_category']=t.channel.astype(str)+' / '+t.category
    out=result(t,'channel_category','median_spend','Source monetary units','Medians describe a typical customer per category; summing category medians is not a median customer total.')
    return out

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