PROJECT 066 · PYTHON SOURCE
Completed-session purchase classification
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': 66, 'dataset': 'shoppers', 'title': 'Completed-session purchase classification', 'question': 'How well can a model classify purchase sessions without PageValues?', 'method': 'Fixed grouped-input train/test benchmark with a prior baseline.', 'action': 'Use the benchmark to study session behavior; obtain time-stamped partial-session data for any earlier intervention.', '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()
t,p,e=classification(df,SHOP_FEATURES,'revenue')
out=result(t,'model','average_precision','Average precision','This is completed-session classification, not real-time conversion forecasting. Exact input duplicates are grouped.',extra={'predictions':p},evaluation=e)
return out
if __name__ == "__main__":
execute(Path(__file__).parent, META, analyze)