← Back to case study

PROJECT 071 · PYTHON SOURCE

Engine configuration comparison

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': 71, 'dataset': 'auto', 'title': 'Engine configuration comparison', 'question': 'How do cylinder groups differ in observed fuel economy and vehicle weight?', 'method': 'Cylinder-level sample sizes and median specifications.', 'action': 'Evaluate comparable vehicle configurations rather than interpreting a cylinder category as an isolated cause.', 'limitations': "Historical city-cycle vehicle observations are not current fleet performance. Vehicle mix and model year are confounded. A '?' horsepower value means missing, not zero. "}

def analyze():
    df = load('auto').copy()
    t=df.groupby('cylinders').agg(vehicles=('row_id','size'),median_mpg=('mpg','median'),median_weight=('weight','median'),median_horsepower=('horsepower','median')).reset_index()
    out=result(t,'cylinders','median_mpg','Miles per US gallon','Rare cylinder groups have few observations. Missing horsepower is not filled for descriptive medians.')
    return out

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