← Back to case study

PROJECT 067 · PYTHON SOURCE

Page value information audit

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': 67, 'dataset': 'shoppers', 'title': 'Page value information audit', 'question': 'How does PageValues alter retrospective purchase classification?', 'method': 'Paired diagnostic models with grouping held fixed to the base features.', 'action': 'Exclude downstream outcome information from any deployable conversion model.', 'limitations': 'One observation is a completed session. End-of-session behavior cannot support an early-session prediction claim. PageValues is excluded from purchase models because it can encode downstream purchase information. Associations are not experiment results. '}

def analyze():
    df = load('shoppers').copy()
    a,pa,ea=classification(df,SHOP_FEATURES,'revenue',groups=SHOP_FEATURES)
    b,pb,eb=classification(df,SHOP_FEATURES+['pagevalues'],'revenue',groups=SHOP_FEATURES)
    assert pa.row_id.equals(pb.row_id)
    t=pd.concat([a.assign(information='Without PageValues'),b.assign(information='Includes PageValues')]);t['variant']=t.information+' / '+t.model
    out=result(t,'variant','average_precision','Average precision','The extra-information variant is a leakage investigation, not a recommended feature set.',evaluation=ea)
    return out

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