PROJECT 043 · PYTHON SOURCE
Mix measurement redundancy
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': 43, 'dataset': 'concrete', 'title': 'Mix measurement redundancy', 'question': 'Which input variables carry overlapping rank information?', 'method': 'Pairwise Spearman associations across mix ingredients and curing age.', 'action': 'Inspect redundancy before simplifying a measurement or modeling workflow.', 'limitations': 'Laboratory observations do not certify a construction mix. Composition and curing conditions are confounded. Models require independent engineering validation and cannot replace strength testing. '}
def analyze():
df = load('concrete').copy()
rows=[]
for i,a in enumerate(CONCRETE_FEATURES):
for b in CONCRETE_FEATURES[i+1:]:
rho=df[[a,b]].corr(method='spearman').iloc[0,1];rows.append({'pair':a+' / '+b,'rho':rho,'absolute_rho':abs(rho)})
t=pd.DataFrame(rows).sort_values('absolute_rho',ascending=False)
out=result(t,'pair','rho','Spearman correlation','Mixture components are constrained jointly. Pairwise correlation does not show that one measurement can safely replace another.')
return out
if __name__ == "__main__":
execute(Path(__file__).parent, META, analyze)