PROJECT 027 · PYTHON SOURCE
Wine quality label balance
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': 27, 'dataset': 'wine', 'title': 'Wine quality label balance', 'question': 'How strongly are sensory labels concentrated in the middle of the rating scale?', 'method': 'Counts and proportions by wine type and observed quality score.', 'action': 'Use label prevalence to choose evaluation metrics before training a quality 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()
t=df.groupby(['wine_type','quality']).size().rename('samples').reset_index()
t['within_type_share']=t.samples/t.groupby('wine_type').samples.transform('sum')
t['label']=t.wine_type+' / '+t.quality.astype(str)
out=result(t,'label','within_type_share','Within-type sample share','The table reports observed score support; absent ratings do not imply impossible products.')
return out
if __name__ == "__main__":
execute(Path(__file__).parent, META, analyze)