PROJECT 069 · PYTHON SOURCE
Historical fuel economy trend
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': 69, 'dataset': 'auto', 'title': 'Historical fuel economy trend', 'question': 'How does observed city-cycle fuel economy vary by model year?', 'method': 'Model-year sample counts, means and medians.', 'action': 'Separate vehicle-composition changes from within-design improvements before explaining a trend.', '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('model_year').mpg.agg(vehicles='size',mean_mpg='mean',median_mpg='median').reset_index();t['calendar_year']=1900+t.model_year
out=result(t,'calendar_year','mean_mpg','Miles per US gallon','These are sampled historical models, not sales-weighted fleet statistics.',kind='line')
return out
if __name__ == "__main__":
execute(Path(__file__).parent, META, analyze)