File size: 10,909 Bytes
2532605 46c1c8b 2532605 46c1c8b 2532605 46c1c8b 2532605 46c1c8b 2532605 46c1c8b 2532605 46c1c8b 2532605 | 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | """
KRONECTOR - Feature engineering for model training.
This module turns the merged race dataset into numeric model inputs while
keeping chronological ordering intact for time-series validation.
"""
from __future__ import annotations
from dataclasses import dataclass
import pickle
from typing import Iterable
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import TimeSeriesSplit
try:
from data import _get_track_type
except ImportError: # pragma: no cover - defensive fallback for isolated use
_get_track_type = None
TARGET_COLUMN = "win_probability"
BASE_REQUIRED_COLUMNS = {
"season",
"round",
"driver_id",
"team",
"grid_position",
"finish_position",
"circuit_id",
}
SECTOR_COLUMNS = ["sector_1_time", "sector_2_time", "sector_3_time"]
NUMERIC_FEATURES = [
# ── Pre-race features only ──
# Grid & qualifying
"season",
"grid_position",
"sector_1_time",
"sector_2_time",
"sector_3_time",
"sector_1_time_era_norm",
"sector_2_time_era_norm",
"sector_3_time_era_norm",
"avg_lap_time_practice",
# Driver & championship context
"championship_standing",
"driver_form_last3",
# Circuit context
"safety_car_probability",
"telemetry_available",
"pole_conversion_rate",
# Driver experience
"career_race_starts",
# NOTE: Race-day features removed (tire_compound, tire_age_laps,
# fresh_tire, pit_stop_count, team_pit_speed, weather_temp_track,
# weather_rainfall) — these cause data leakage for pre-race predictions.
]
CATEGORICAL_FEATURES = ["team", "track_type", "regulation_era"]
UNKNOWN_CATEGORY = "unknown"
LEAKAGE_COLUMNS = {
"finish_position",
"driver_name",
"driver_id",
"circuit_id",
TARGET_COLUMN,
# Race-day features that we don't have before the race
"tire_compound",
"tire_age_laps",
"fresh_tire",
"pit_stop_count",
"team_pit_speed",
"weather_temp_track",
"weather_rainfall",
}
EXCLUDED_FEATURE_COLUMNS = LEAKAGE_COLUMNS | {"round"}
@dataclass(frozen=True)
class FeatureBundle:
"""Container returned by prepare_model_data."""
X: pd.DataFrame
y: pd.Series
metadata: pd.DataFrame
feature_columns: list[str]
def validate_input_schema(df: pd.DataFrame) -> None:
"""Raise ValueError if the minimum training schema is missing."""
missing = BASE_REQUIRED_COLUMNS - set(df.columns)
if missing:
raise ValueError(f"Missing required columns: {sorted(missing)}")
def ensure_training_columns(df: pd.DataFrame) -> pd.DataFrame:
"""
Add derived/default columns expected by feature engineering.
The preferred input is the merged dataset from data.merge_datasets. This
helper also accepts the current FastF1-only parquet for smoke training.
"""
validate_input_schema(df)
result = df.copy()
if TARGET_COLUMN not in result.columns:
result[TARGET_COLUMN] = (result["finish_position"] == 1).astype(int)
if "regulation_era" not in result.columns:
result["regulation_era"] = np.where(
result["season"] >= 2026, "agile_era",
np.where(result["season"] >= 2022, "ground_effect_era", "hybrid_era")
)
if "track_type" not in result.columns:
if _get_track_type is None:
result["track_type"] = "permanent"
else:
result["track_type"] = result["circuit_id"].apply(_get_track_type)
defaults = {
"championship_standing": np.nan,
"driver_form_last3": np.nan,
"safety_car_probability": 0.0,
"telemetry_available": False,
"avg_lap_time_practice": np.nan,
"tire_compound": np.nan,
"tire_age_laps": np.nan,
"fresh_tire": np.nan,
"pit_stop_count": np.nan,
"team_pit_speed": np.nan,
"weather_temp_track": np.nan,
"weather_rainfall": np.nan,
}
for column, default in defaults.items():
if column not in result.columns:
result[column] = default
for column in SECTOR_COLUMNS:
if column not in result.columns:
result[column] = np.nan
return result
def add_era_normalized_sector_times(df: pd.DataFrame) -> pd.DataFrame:
"""
Add z-scored sector columns normalized within regulation era.
Normalizing within era avoids mixing hybrid-era and ground-effect-era lap
profiles. Zero standard deviation is treated as 1.0 to avoid division by 0.
"""
result = df.copy()
for column in SECTOR_COLUMNS:
norm_column = column.replace("_time", "_time_era_norm")
grouped = result.groupby("regulation_era")[column]
mean = grouped.transform("mean")
std = grouped.transform("std").replace(0, 1.0).fillna(1.0)
result[norm_column] = (result[column] - mean) / std
return result
def add_driver_form(df: pd.DataFrame) -> pd.DataFrame:
"""
Compute driver_form_last3 without leaking the current race result.
The calculation sorts by (driver_id, season, round), then uses
shift(1).rolling(3).mean() so each row only sees prior races.
"""
result = df.copy().reset_index(drop=True)
sorted_df = result.sort_values(["driver_id", "season", "round"]).copy()
form = (
sorted_df.groupby("driver_id")["finish_position"]
.transform(lambda x: x.shift(1).rolling(3, min_periods=1).mean())
)
result.loc[sorted_df.index, "driver_form_last3"] = form
return result
def _impute_championship_standing(result: pd.DataFrame) -> pd.DataFrame:
"""Fill missing standings with the worst known standing in that season."""
result["championship_standing"] = pd.to_numeric(
result["championship_standing"], errors="coerce"
)
result["championship_standing"] = result.groupby("season")[
"championship_standing"
].transform(lambda x: x.fillna(x.max()))
if result["championship_standing"].isna().any():
global_max = result["championship_standing"].max()
fill_value = 0.0 if pd.isna(global_max) else global_max
result["championship_standing"] = result[
"championship_standing"
].fillna(fill_value)
return result
def impute_missing_values(df: pd.DataFrame) -> pd.DataFrame:
"""Impute numeric and categorical missing values deterministically."""
result = df.copy()
result = _impute_championship_standing(result)
for column in NUMERIC_FEATURES:
if column not in result.columns:
result[column] = np.nan
if result[column].dtype == bool:
result[column] = result[column].astype(int)
continue
result[column] = pd.to_numeric(result[column], errors="coerce")
valid_values = result[column].dropna()
if valid_values.empty:
median = 0.0
else:
median = valid_values.median()
result[column] = result[column].fillna(median)
for column in CATEGORICAL_FEATURES:
if column not in result.columns:
result[column] = UNKNOWN_CATEGORY
result[column] = result[column].fillna(UNKNOWN_CATEGORY).astype(str)
return result
def fit_label_encoders(df: pd.DataFrame) -> dict[str, LabelEncoder]:
"""Fit LabelEncoders for all configured categorical features."""
encoders = {}
for column in CATEGORICAL_FEATURES:
values = df[column].fillna(UNKNOWN_CATEGORY).astype(str)
values = pd.concat([values, pd.Series([UNKNOWN_CATEGORY])], ignore_index=True)
encoder = LabelEncoder()
encoder.fit(values)
encoders[column] = encoder
return encoders
def encode_categoricals(
df: pd.DataFrame, encoders: dict[str, LabelEncoder] | None = None
) -> tuple[pd.DataFrame, dict[str, LabelEncoder]]:
"""
Label-encode categorical features.
If encoders are provided, they are reused for inference. Unknown inference
values are mapped to the explicit "unknown" class fitted during training.
"""
result = df.copy()
fitted_encoders = encoders or fit_label_encoders(result)
for column in CATEGORICAL_FEATURES:
if column not in fitted_encoders:
raise ValueError(f"Missing fitted encoder for categorical column: {column}")
encoder = fitted_encoders[column]
known_classes = set(encoder.classes_)
values = result[column].fillna(UNKNOWN_CATEGORY).astype(str)
values = values.where(values.isin(known_classes), UNKNOWN_CATEGORY)
result[column] = encoder.transform(values)
return result, fitted_encoders
def save_encoders(encoders: dict[str, LabelEncoder], path: str) -> None:
"""Persist fitted categorical encoders for model inference."""
with open(path, "wb") as file:
pickle.dump(encoders, file)
def load_encoders(path: str) -> dict[str, LabelEncoder]:
"""Load fitted categorical encoders saved by save_encoders."""
with open(path, "rb") as file:
return pickle.load(file)
def prepare_model_data(
df: pd.DataFrame, encoders: dict[str, LabelEncoder] | None = None
) -> tuple[FeatureBundle, dict[str, LabelEncoder]]:
"""
Build model-ready X/y from a race dataset.
The returned frame is sorted by (season, round, grid_position), and leakage
columns such as finish_position are excluded from X.
"""
prepared = ensure_training_columns(df)
prepared = prepared.sort_values(["season", "round", "grid_position"]).reset_index(
drop=True
)
if prepared["driver_form_last3"].isna().all():
prepared = add_driver_form(prepared)
prepared = add_era_normalized_sector_times(prepared)
prepared = impute_missing_values(prepared)
metadata_columns = [
column
for column in ["season", "round", "driver_id", "driver_name", "team", "grid_position", "quali_status"]
if column in prepared.columns
]
metadata = prepared[metadata_columns].copy()
prepared, fitted_encoders = encode_categoricals(prepared, encoders)
y = prepared[TARGET_COLUMN].astype(int)
feature_columns = [
column
for column in prepared.columns
if column not in EXCLUDED_FEATURE_COLUMNS
and pd.api.types.is_numeric_dtype(prepared[column])
]
X = prepared[feature_columns].copy()
return (
FeatureBundle(
X=X,
y=y,
metadata=metadata,
feature_columns=feature_columns,
),
fitted_encoders,
)
def create_time_series_splits(
X: pd.DataFrame, n_splits: int = 5
) -> Iterable[tuple[np.ndarray, np.ndarray]]:
"""Return chronological TimeSeriesSplit indices."""
if len(X) <= n_splits:
raise ValueError(
f"Need more rows than n_splits; got {len(X)} rows and {n_splits} splits"
)
splitter = TimeSeriesSplit(n_splits=n_splits)
return splitter.split(X)
|