← Back to case study

PROJECT 089 · PYTHON SOURCE

Compact inspection feature set

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': 89, 'dataset': 'steel', 'title': 'Compact inspection feature set', 'question': 'How much recognition performance changes when only compact geometric features are retained?', 'method': 'Paired fixed-model ablation using the same full-signature input groups.', 'action': 'Compare measurement cost and external validation results before reducing the inspection feature set.', '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()
    compact=['pixels_areas','x_perimeter','y_perimeter','square_index','orientation_index']
    a,pa,ea=classification(df,compact,'fault',groups=STEEL_FEATURES)
    b,pb,eb=classification(df,STEEL_FEATURES,'fault',groups=STEEL_FEATURES)
    assert pa.row_id.equals(pb.row_id)
    t=pd.concat([a.assign(features='Compact'),b.assign(features='Full')]);t['variant']=t.features+' / '+t.model
    out=result(t,'variant','macro_f1','Macro F1','Feature sets were specified before scoring. The full-signature grouping does not guarantee unseen machine or batch generalization.',evaluation=eb)
    return out

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