← Back to case study

PROJECT 040 · PYTHON SOURCE

Superplasticizer and water demand

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': 40, 'dataset': 'concrete', 'title': 'Superplasticizer and water demand', 'question': 'How does water content differ across observed superplasticizer dosage bands?', 'method': 'Dosage groups with water and strength summaries.', 'action': 'Design matched laboratory comparisons before inferring a dosage effect.', '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()
    df['dosage_band']=pd.cut(df.superplasticizer,[-.001,0,5,10,15,40]).astype(str)
    t=df.groupby('dosage_band').agg(samples=('water','size'),median_water=('water','median'),median_strength=('strength','median')).reset_index()
    out=result(t,'dosage_band','median_water','kg per cubic metre','Mixtures were not randomly assigned to dosage bands. The source cannot establish optimal dosage.')
    return out

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