← Back to case study

PROJECT 074 · PYTHON SOURCE

Later-model fuel economy transfer

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': 74, 'dataset': 'auto', 'title': 'Later-model fuel economy transfer', 'question': 'How do specification-based models perform on later model years?', 'method': 'Chronological-by-model-year evaluation; fixed baseline and regression models.', 'action': 'Compare temporal transfer with random grouped evaluation to understand extrapolation limits.', '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()
    d=df.sort_values(['model_year','row_id']).copy()
    t,p,e=regression(d,AUTO_FEATURES,'mpg',split='ordered')
    out=result(t,'model','mae','MPG MAE','The split is by ordered records and may split the boundary year; it does not imply exact release-date chronology.',extra={'predictions':p},evaluation=e)
    return out

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