PROJECT 042 · PYTHON SOURCE
Strength prediction by curing age
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': 42, 'dataset': 'concrete', 'title': 'Strength prediction by curing age', 'question': 'At which curing ages are the fixed strength model errors largest?', 'method': 'Stratify grouped-holdout residuals by prespecified curing-age bands.', 'action': 'Review error patterns across testing ages when specifying an external validation study.', '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()
scores,p,e=regression(df,CONCRETE_FEATURES,'strength',groups=CONCRETE_FEATURES[:-1])
p=p.merge(df[['row_id','age']],on='row_id');p['band']=pd.cut(p.age,[0,7,28,90,np.inf]).astype(str);p['absolute_error']=abs(p['Extra trees']-p.actual)
t=p.groupby('band').agg(test_samples=('row_id','size'),mae=('absolute_error','mean')).reset_index()
out=result(t,'band','mae','MPa MAE','Small holdout groups make age-specific averages unstable; this is a diagnostic, not a new independent test.',extra={'model_scores':scores},evaluation=e)
return out
if __name__ == "__main__":
execute(Path(__file__).parent, META, analyze)