sleonca's picture
Upload folder using huggingface_hub
b47743b verified
Raw History Blame Contribute Delete
1.68 kB
"""Forecast 2019-2025 from data up to 2018-12-31 with the current model alone (final_model.py).
Until 2026-11-08 this was submission.csv. It now writes results/submission_current_model.csv; the submitted
file (ARIMA ensemble B, which uses this model for three metrics and 75 % of temperature) is written by
code/04_forecast_submission.py.
"""
import warnings; warnings.filterwarnings("ignore")
import numpy as np, pandas as pd
from common import ROOT, METRICS, CUTOFF_YEAR, load_annual, long_valid
import final_model
A = load_annual()
LV = long_valid(A)
assert LV.Year.max() == CUTOFF_YEAR
COORDS = A.groupby("ID")[["Lat", "Lon"]].first()
YEARS = list(range(2019, 2026))
pred = pd.concat([final_model.forecast(LV, CUTOFF_YEAR, YEARS, COORDS, m) for m in METRICS])
# fallback for any city-metric the models could not fill: mean of all valid years
longrun = LV[LV.valid].groupby(["ID", "metric"]).value.mean().rename("lr").reset_index()
pred = pred.merge(longrun, on=["ID", "metric"], how="left")
n_fb = pred.pred.isna().sum()
pred["pred"] = pred.pred.fillna(pred.lr)
print("fallback rows:", n_fb)
tmpl = pd.read_csv(ROOT / "submission.csv", dtype={"city_id": str})[["city_id", "year", "metric"]]
sub = tmpl.merge(pred.rename(columns={"ID": "city_id", "Year": "year", "pred": "predicted_value"})[["city_id", "year", "metric", "predicted_value"]],
on=["city_id", "year", "metric"], how="left")
assert len(sub) == 5880 and sub.predicted_value.notna().all()
sub["predicted_value"] = sub.predicted_value.round(3)
sub.to_csv(ROOT / "results" / "submission_current_model.csv", index=False)
print(sub.groupby(["metric", "year"]).predicted_value.mean().unstack().round(2))