← Back to case study

PROJECT 098 · PYTHON SOURCE

Non-destructive measurement tradeoff

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': 98, 'dataset': 'abalone', 'title': 'Non-destructive measurement tradeoff', 'question': 'How does restricting predictors to external measurements change ring-count error?', 'method': 'Paired ablation of external-only versus full measurements with common input-group splits.', 'action': 'Compare measurement effort and external accuracy before proposing a field workflow.', 'limitations': 'Physical measurements use source scaling. Rings are the modeled target; the source describes an approximate age conversion. Site and animal group identifiers are unavailable, limiting biological generalization. '}

def analyze():
    df = load('abalone').copy()
    external=['sex','length','diameter','height','whole_weight']
    a,pa,ea=regression(df,external,'rings',groups=ABALONE_FEATURES)
    b,pb,eb=regression(df,ABALONE_FEATURES,'rings',groups=ABALONE_FEATURES)
    assert pa.row_id.equals(pb.row_id)
    t=pd.concat([a.assign(measurements='External'),b.assign(measurements='Full')]);t['variant']=t.measurements+' / '+t.model
    out=result(t,'variant','mae','Ring-count MAE','External measurements are chosen by measurement availability; field robustness is not established.',evaluation=eb)
    return out

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