PROJECT 035 · PYTHON SOURCE
Quality model errors by 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': 35, 'dataset': 'wine', 'title': 'Quality model errors by wine type', 'question': 'Does a pooled quality model have different error or bias for red and white samples?', 'method': 'Held-out residual summaries stratified by wine type for a fixed Extra Trees model.', 'action': 'Investigate subgroup calibration and obtain external batch-level validation before specialization.', '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()
scores,p,e=regression(df,WINE_FEATURES,'quality')
p=p.merge(df[['row_id','wine_type']],on='row_id');p['absolute_error']=abs(p['Extra trees']-p.actual);p['error']=p['Extra trees']-p.actual
t=p.groupby('wine_type').agg(test_samples=('row_id','size'),mae=('absolute_error','mean'),bias=('error','mean')).reset_index()
out=result(t,'wine_type','mae','Sensory-score MAE','This is a subgroup diagnostic of the shared holdout, not a separate independent validation.',extra={'model_scores':scores},evaluation=e)
return out
if __name__ == "__main__":
execute(Path(__file__).parent, META, analyze)