← Back to case study

PROJECT 030 · PYTHON SOURCE

Sugar and density measurement structure

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': 30, 'dataset': 'wine', 'title': 'Sugar and density measurement structure', 'question': 'How does density vary across residual-sugar bands within wine type?', 'method': 'Stratified median density and interquartile ranges.', 'action': 'Use measurement structure to check plausibility and collinearity in a laboratory model.', 'limitations': 'Sensory scores are ordinal and concentrated in the middle. Producer and batch IDs are unavailable. Associations are not recipes for changing quality or evidence of market price. '}

def analyze():
    df = load('wine').copy()
    df['sugar_band']=pd.cut(df.residual_sugar,[0,2,5,10,20,np.inf]).astype(str)
    t=df.groupby(['wine_type','sugar_band']).density.agg(samples='size',median_density='median',p25=lambda s:s.quantile(.25),p75=lambda s:s.quantile(.75)).reset_index()
    t=t[t.samples>=20];t['group']=t.wine_type+' / '+t.sugar_band
    out=result(t,'group','median_density','Source density units','Bands are prespecified and sparse groups are excluded; density differences are not quality improvements.')
    return out

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