PROJECT 028 · PYTHON SOURCE
Alcohol and quality within wine type
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': 28, 'dataset': 'wine', 'title': 'Alcohol and quality within wine type', 'question': 'How does median sensory quality vary across declared alcohol bands within red and white samples?', 'method': 'Stratified alcohol bands with sample counts and quality medians.', 'action': 'Use the stratified pattern to motivate a controlled laboratory study rather than a recipe claim.', '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['alcohol_band']=pd.cut(df.alcohol,[0,9,10,11,12,13,20]).astype(str)
t=df.groupby(['wine_type','alcohol_band']).quality.agg(samples='size',median_quality='median',mean_quality='mean').reset_index()
t=t[t.samples>=30];t['group']=t.wine_type+' / '+t.alcohol_band
out=result(t,'group','mean_quality','Sensory score','Require at least 30 samples per band. Other chemical attributes differ between these groups.')
return out
if __name__ == "__main__":
execute(Path(__file__).parent, META, analyze)