File size: 5,150 Bytes
3ff1d30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""Run the contest's starter code (random forest on 3 lags) and score it like the leaderboard

(live years 2019-2021). Scoring uses NOAA 2019-2021 observations (code/truth.py): ANALYSIS ONLY,

never a forecast input, and the forecast below is frozen before it is read.

Usage: python code/starter_score.py OBS_CSV   (OBS_CSV from code/fetch_truth.py)"""
import sys, warnings
from pathlib import Path
import numpy as np, pandas as pd
from sklearn.ensemble import RandomForestRegressor
sys.path.insert(0, str(Path(__file__).resolve().parent))
from truth import annual_from_daily
from shared import METRICS, load_train, valid_flags as _valid_flags
warnings.filterwarnings("ignore")

LIVE = [2019, 2020, 2021]
HDR = ["Score", "Avg temp MAE °F", "Precip MAE in", "Hottest day MAE °F", "CDD MAE °F-days"]
EXP02_SUBMISSION = Path(__file__).resolve().parent.parent / "outputs" / "exp02" / "submission_exp02.csv"

# ---- 1. data (<= 2018, cutoff guard, inf -> NaN: see shared.py) --------------------------
train_df, cov = load_train()

# ---- 2/3. starter code: lag features + RandomForest, iterative 2019-2025 ----------------
def starter_forecast(target):
    df = train_df[["ID", "Year", target]].copy()
    for k in (1, 2, 3):
        df[f"lag_{k}"] = df.groupby("ID")[target].shift(k)
    features = ["lag_1", "lag_2", "lag_3"]
    fit_df = df.dropna(subset=features + [target])
    model = RandomForestRegressor(n_estimators=100, random_state=42)
    model.fit(fit_df[features], fit_df[target])
    out = {}
    for cid, g in df.groupby("ID"):
        hist = list(g[target].ffill().values[-3:])  # ffill: a few cities end 2018 with a missing value
        preds = []
        for _ in range(7):
            p = float(model.predict(pd.DataFrame([hist[::-1][:3]], columns=features))[0])
            preds.append(p); hist.append(p)
        out[cid] = preds
    return out

# ---- baselines (official definitions + the leaderboard's 10-year mean) -----------------
def valid_flags(a):
    return _valid_flags(a, cov)

flags = valid_flags(train_df)
base = {k: {} for k in ("30-year average", "10-year mean", "last year repeated", "linear trend")}
baseline_ok = {}
for m, col in METRICS.items():
    d = train_df.assign(v=train_df[col].where(flags[m]))
    for cid, g in d.groupby("ID"):
        s = g.set_index("Year")["v"].dropna()
        w = s[s.index >= 1989]
        baseline_ok[(cid, m)] = len(w) >= 10
        if len(w) == 0: continue
        slope, icpt = np.polyfit(w.index.values.astype(float), w.values, 1) if len(w) > 1 else (0, w.mean())
        base["30-year average"][(cid, m)] = np.full(7, w.mean())
        base["10-year mean"][(cid, m)] = np.full(7, s[s.index >= 2009].mean())
        base["last year repeated"][(cid, m)] = np.full(7, s.iloc[-1])
        base["linear trend"][(cid, m)] = icpt + slope * np.arange(2019, 2026)

# ---- truth (NOAA 2019-2021, analysis only) ----------------------------------------------
obs = pd.read_csv(sys.argv[1], dtype={"DATE": str})
truth = annual_from_daily(obs)
tv = {"annual_avg_temp": ("avg_temp", np.maximum(truth.miss_tmax, truth.miss_tmin)), "total_precip_in": ("tot_prcp", truth.miss_prcp),
      "hottest_day_temp": ("max_tmax", truth.miss_tmax), "cdd": ("cdd", truth.miss_both)}
truth = truth[truth.year.isin(LIVE)]

def score(fc):  # fc[(cid, metric)] -> 7 values for 2019..2025
    mae = {}
    for m, (col, miss) in tv.items():
        t = truth.assign(miss=miss.loc[truth.index])
        t = t[(t.miss <= 10) & t[col].notna()]
        errs = [abs(fc[(r.ID, m)][r.year - 2019] - getattr(r, col)) for r in t.itertuples()
                if (r.ID, m) in fc and baseline_ok.get((r.ID, m))]
        mae[m] = float(np.mean(errs))
    return mae

models = {"Starter code (RandomForest)": {}}
for target_m, col in METRICS.items():
    for cid, p in starter_forecast(col).items():
        models["Starter code (RandomForest)"][(cid, target_m)] = p
models.update(base)
OUT = Path(__file__).resolve().parent.parent / "outputs"
for label, path in [("Line + average (exp02)", EXP02_SUBMISSION),  # frozen forecasts written earlier by the experiments
                    ("Exp03 chosen per metric (analysis only)", OUT / "exp03" / "submission_exp03.csv"),
                    ("Shared slope (all metrics) (analysis only)", OUT / "exp03" / "submission_exp03_shared_all.csv"),
                    ("Half slope (all metrics) (analysis only)", OUT / "exp03" / "submission_exp03_half_all.csv")]:
    if path.exists():
        sub = pd.read_csv(path, dtype={"city_id": str})
        models[label] = {k: g.sort_values("year").predicted_value.tolist() for k, g in sub.groupby(["city_id", "metric"])}

res = {k: score(v) for k, v in models.items()}
ref = res["30-year average"]
print(f"{'Model':<44}" + "".join(f"{h:>20}" for h in HDR))
for k, mae in res.items():
    S = np.mean([1 - mae[m] / ref[m] for m in METRICS])
    print(f"{k:<44}{100 * max(0, S):>20.1f}" + "".join(f"{mae[m]:>20.3f}" for m in METRICS))
print("\nLeaderboard reference '10-year mean · example notebook': 10.4  1.191  5.782  2.295  149.6")