PROJECT 082 · PYTHON SOURCE
Atypical wholesale purchasing profiles
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': 82, 'dataset': 'wholesale', 'title': 'Atypical wholesale purchasing profiles', 'question': 'Which customer spending profiles are unusual relative to this sample?', 'method': 'Isolation Forest on log-transformed standardized category spending, with a declared 5% review fraction.', 'action': 'Review atypical profiles for data quality or legitimate specialized purchasing; do not label them fraudulent.', '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()
x=StandardScaler().fit_transform(np.log1p(df[SPEND]))
model=IsolationForest(n_estimators=100,contamination=.05,random_state=42,n_jobs=1)
df['review_score']=-model.fit(x).score_samples(x)
t=df[['row_id',*SPEND,'review_score']].sort_values('review_score',ascending=False)
out=result(t,'row_id','review_score','Relative anomaly score','There are no anomaly labels. The review fraction is a workflow assumption, not a measured error rate.')
return out
if __name__ == "__main__":
execute(Path(__file__).parent, META, analyze)