← Back to case study

PROJECT 085 · PYTHON SOURCE

Thickness and fault composition

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': 85, 'dataset': 'steel', 'title': 'Thickness and fault composition', 'question': 'How does the composition of recorded faults vary across plate-thickness bands?', 'method': 'Thickness strata and within-stratum fault shares.', 'action': 'Investigate process mix before attributing differences to plate thickness.', 'limitations': 'Every source row is a recorded fault. The data cannot estimate a production defect rate or distinguish healthy plates. Batch and machine IDs are absent, so grouped exact-input holdouts do not prove factory transfer. '}

def analyze():
    df = load('steel').copy()
    df['thickness_band']=pd.cut(df.steel_plate_thickness,[0,50,100,150,200,np.inf]).astype(str)
    t=df.groupby(['thickness_band','fault']).size().rename('records').reset_index();t['within_band_share']=t.records/t.groupby('thickness_band').records.transform('sum')
    t['group']=t.thickness_band+' / '+t.fault
    out=result(t,'group','within_band_share','Within-band fault share','Healthy production exposure is unavailable, so these shares cannot establish thickness-specific defect risk.')
    return out

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