← Back to case study

PROJECT 033 · PYTHON SOURCE

Higher-rated wine screening

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': 33, 'dataset': 'wine', 'title': 'Higher-rated wine screening', 'question': 'Can chemistry rank samples with an observed quality score of at least seven?', 'method': 'Binary target specified before analysis; grouped-input classification against prior prevalence.', 'action': 'Evaluate a prospective screening workflow with external batches before using the ranking.', '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['higher_rated']=df.quality.ge(7).astype(int)
    t,p,e=classification(df,WINE_FEATURES,'higher_rated')
    out=result(t,'model','average_precision','Average precision','Seven is an analytical threshold, not a universal commercial standard. Quality itself is excluded from predictors.',extra={'predictions':p},evaluation=e)
    return out

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