PROJECT 025 · PYTHON SOURCE
Prior-contact recency and response
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': 25, 'dataset': 'bank', 'title': 'Prior-contact recency and response', 'question': 'How does response differ between never-contacted clients and prior-contact recency bands?', 'method': 'Preserve pdays=-1 as a sentinel category before computing rate intervals.', 'action': 'Keep never-contacted records distinct when evaluating engagement history.', 'limitations': 'Observational marketing records do not identify campaign uplift. Month and row order are not precise timestamps; repeated-client identifiers are unavailable. Models are research diagnostics, not financial eligibility or automated contact decisions. '}
def analyze():
df = load('bank').copy()
df['recency_band']=pd.cut(df.pdays,[0,30,90,180,365,np.inf],include_lowest=True).astype(str)
df.loc[df.pdays==-1,'recency_band']='Never previously contacted'
t=rates(df,'recency_band','subscribed')
out=result(t,'recency_band','rate','Subscription share','The -1 sentinel is not a negative elapsed time. Small groups have wider uncertainty.')
return out
if __name__ == "__main__":
execute(Path(__file__).parent, META, analyze)