PROJECT 034 · PYTHON SOURCE
Repeated laboratory signature audit
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': 34, 'dataset': 'wine', 'title': 'Repeated laboratory signature audit', 'question': 'How often do identical laboratory signatures recur, and can their sensory scores disagree?', 'method': 'Group exact predictor signatures and count score multiplicity without treating repeated rows as proven errors.', 'action': 'Preserve input groups in evaluation and investigate the meaning of repeats before deduplicating.', '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()
g=df.groupby(WINE_FEATURES,dropna=False).quality.agg(records='size',distinct_scores='nunique').reset_index()
g['signature_class']=np.select([g.records.eq(1),g.distinct_scores.eq(1)],['Single record','Repeated with same score'],default='Repeated with differing scores')
t=g.groupby('signature_class').agg(signatures=('records','size'),source_rows=('records','sum')).reset_index()
out=result(t,'signature_class','source_rows','Source rows','No batch identifiers are supplied; identical inputs need not be duplicate ingestion errors.')
return out
if __name__ == "__main__":
execute(Path(__file__).parent, META, analyze)