← Back to case study

PROJECT 073 · PYTHON SOURCE

Fuel economy prediction

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': 73, 'dataset': 'auto', 'title': 'Fuel economy prediction', 'question': 'Can technical specifications estimate historical city-cycle fuel economy?', 'method': 'Fixed grouped-input regression with median and linear baselines.', 'action': 'Validate on new vehicle designs and test conditions before making engineering predictions.', '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,p,e=regression(df,AUTO_FEATURES,'mpg')
    out=result(t,'model','mae','MPG MAE','Car names and row identifiers are excluded. Horsepower imputation is fitted only on training observations.',extra={'predictions':p},evaluation=e)
    return out

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