Isole les features locales des labels et lance un protocole temporel propre
This commit is contained in:
56
clean_features.py
Normal file
56
clean_features.py
Normal file
@@ -0,0 +1,56 @@
|
||||
"""Features pompe seules. Aucun import de cibles, accès base ou données futures."""
|
||||
|
||||
from datetime import date, timedelta
|
||||
from statistics import median
|
||||
|
||||
HORIZONS = (1, 3, 7, 14, 21)
|
||||
|
||||
|
||||
def features_asof(prefix, prediction_time, exclude=()):
|
||||
"""Reçoit exclusivement des états déjà disponibles. Rejette tout préfixe futur."""
|
||||
assert all(p["available_at"] <= prediction_time for p in prefix)
|
||||
points = {p["day"]: p for p in prefix}
|
||||
if not prefix:
|
||||
return None
|
||||
current = prefix[-1]
|
||||
day = date.fromisoformat(current["day"])
|
||||
values, manifest = {}, []
|
||||
for lag in (1, 3, 7, 14, 30):
|
||||
name = f"past_delta_{lag}"
|
||||
if name in exclude:
|
||||
continue
|
||||
previous = points.get((day - timedelta(days=lag)).isoformat())
|
||||
if previous is None:
|
||||
return None
|
||||
shared = current["prices"].keys() & previous["prices"].keys()
|
||||
if len(shared) < 3:
|
||||
return None
|
||||
values[name] = median((current["prices"][s] - previous["prices"][s]) * 100 for s in shared)
|
||||
manifest.append(
|
||||
dict(
|
||||
name=name,
|
||||
source="prix-carburants:journal",
|
||||
lookback_days=lag,
|
||||
max_available_at=current["available_at"],
|
||||
calculated=True,
|
||||
allowed_horizons=HORIZONS,
|
||||
availability_estimated=True,
|
||||
)
|
||||
)
|
||||
if "percentile_90" not in exclude:
|
||||
past = [median(p["prices"].values()) for p in prefix if len(p["prices"]) >= 3]
|
||||
level = median(current["prices"].values())
|
||||
values["percentile_90"] = sum(v <= level for v in past) / len(past)
|
||||
manifest.append(
|
||||
dict(
|
||||
name="percentile_90",
|
||||
source="prix-carburants:journal",
|
||||
lookback_days=90,
|
||||
max_available_at=current["available_at"],
|
||||
calculated=True,
|
||||
allowed_horizons=HORIZONS,
|
||||
availability_estimated=True,
|
||||
)
|
||||
)
|
||||
assert all(f["max_available_at"] <= prediction_time for f in manifest)
|
||||
return dict(prediction_time=prediction_time, values=values, manifest=manifest)
|
||||
Reference in New Issue
Block a user