PROJECT 029 · PYTHON SOURCE
Chemistry associations with quality
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': 29, 'dataset': 'wine', 'title': 'Chemistry associations with quality', 'question': 'Which laboratory measurements have the strongest rank associations with quality within each wine type?', 'method': 'Pairwise complete Spearman correlations stratified by wine type.', 'action': 'Prioritize hypotheses for experiments while checking whether pooled relationships mask type differences.', '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()
tables=[]
for kind,g in df.groupby('wine_type'):
a=associations(g,'quality',[c for c in WINE_FEATURES if c!='wine_type']);a['wine_type']=kind;tables.append(a)
t=pd.concat(tables);t['association']=t.wine_type+' / '+t.feature
out=result(t,'association','spearman_rho','Spearman correlation','The table contains both types. Correlations do not adjust for other chemistry or multiple exploratory comparisons.')
return out
if __name__ == "__main__":
execute(Path(__file__).parent, META, analyze)