fix(ml): resolve km_open under-prediction and remove Rule 3

- Removed data leakage features (lifts_open, snow_depth) from ML training.
- Added station_id as an encoded categorical feature to enable station-awareness.
- Removed Rule 3 (warm rain reduction) from the rules engine to avoid double-penalization.
- Retrained all models with the corrected feature set.
This commit is contained in:
dasemu committed 2026-03-13 10:01:43 +01:00
1 parent 6aa37f88e7
commit b5b62e582e
3 files changed
+37 -39

No files matched your search

+17 -2
View File
@@ -30,6 +30,7 @@ _OPEN_MODEL_FILE = "station_open_model.joblib"
_KM_MODEL_FILE = "km_open_model.joblib"
_CROWD_MODEL_FILE = "crowd_level_model.joblib"
_CROWD_ENCODER_FILE = "crowd_level_label_encoder.joblib"
_STATION_ENCODER_FILE = "station_id_label_encoder.joblib"
def _load_model(path: Path) -> Any | None:
@@ -93,6 +94,7 @@ class MLPredictor:
self._km_model: Any | None = None
self._crowd_model: Any | None = None
self._crowd_encoder: Any | None = None
self._station_encoder: Any | None = None
self._loaded = False
if eager_load:
@@ -108,6 +110,7 @@ class MLPredictor:
self._km_model = _load_model(self._models_dir / _KM_MODEL_FILE)
self._crowd_model = _load_model(self._models_dir / _CROWD_MODEL_FILE)
self._crowd_encoder = _load_model(self._models_dir / _CROWD_ENCODER_FILE)
self._station_encoder = _load_model(self._models_dir / _STATION_ENCODER_FILE)
self._loaded = True
available = [
@@ -116,6 +119,7 @@ class MLPredictor:
("open_model", self._open_model),
("km_model", self._km_model),
("crowd_model", self._crowd_model),
("station_encoder", self._station_encoder),
]
if mdl is not None
]
@@ -148,15 +152,26 @@ class MLPredictor:
except AttributeError:
# Fallback: drop known non-feature columns
drop_cols = {
"date", "station_id", "station_open", "open_status",
"date", "station_open", "open_status",
"km_open", "crowd_level", "snow_quality", "season",
"lifts_open", "snow_depth",
}
# Note: station_id is NOT in drop_cols as it's now a feature
expected = [c for c in df.columns if c not in drop_cols]
# Select only expected columns, adding missing ones as 0
result = pd.DataFrame(index=df.index)
for col in expected:
if col in df.columns:
if col == "station_id" and self._station_encoder is not None:
# Use the fitted LabelEncoder for station_id
station_val = str(df["station_id"].iloc[0])
try:
result[col] = self._station_encoder.transform([station_val])[0]
except (ValueError, KeyError):
# Unknown station; fallback to first class as a guess
logger.warning("Unknown station_id '%s' for encoder.", station_val)
result[col] = 0
elif col in df.columns:
result[col] = pd.to_numeric(df[col], errors="coerce").fillna(0.0)
else:
result[col] = 0.0