Spaces:
Runtime error
Runtime error
Download app/training/preprocess.py from devmurtaza/RastaAI: direct link, hf CLI and curl.
- Browser
- Download file 1.48 kB
-
https://huggingface.co/spaces/devmurtaza/RastaAI/resolve/main/app/training/preprocess.py
- Command line
-
hf download hf://spaces/devmurtaza/RastaAI/app/training/preprocess.py
-
curl -L -o preprocess.py https://huggingface.co/spaces/devmurtaza/RastaAI/resolve/main/app/training/preprocess.py
1.48 kB
| """Feature preprocessing pipeline. | |
| Numerical features pass through StandardScaler; categorical features | |
| (road_type, vehicle_type) one-hot encoded. The whole ColumnTransformer is | |
| saved with joblib so inference uses identical encoding to training. | |
| """ | |
| from __future__ import annotations | |
| from typing import List, Tuple | |
| import numpy as np | |
| import pandas as pd | |
| from sklearn.compose import ColumnTransformer | |
| from sklearn.preprocessing import StandardScaler, OneHotEncoder | |
| NUMERIC_FEATURES: List[str] = [ | |
| "distance_km", | |
| "nominal_speed_kmh", | |
| "turns", | |
| "turns_per_km", | |
| "traffic_level", | |
| "road_quality", | |
| "hour_of_day", | |
| "day_of_week", | |
| "weather_severity", | |
| "detour_ratio", | |
| ] | |
| CATEGORICAL_FEATURES: List[str] = ["road_type", "vehicle_type"] | |
| ALL_FEATURES: List[str] = NUMERIC_FEATURES + CATEGORICAL_FEATURES | |
| def build_preprocessor() -> ColumnTransformer: | |
| return ColumnTransformer( | |
| transformers=[ | |
| ("num", StandardScaler(), NUMERIC_FEATURES), | |
| ("cat", OneHotEncoder(handle_unknown="ignore", sparse_output=False), CATEGORICAL_FEATURES), | |
| ], | |
| remainder="drop", | |
| ) | |
| def features_to_dataframe(rows: List[dict]) -> pd.DataFrame: | |
| df = pd.DataFrame(rows) | |
| for col in NUMERIC_FEATURES: | |
| if col not in df.columns: | |
| df[col] = 0.0 | |
| for col in CATEGORICAL_FEATURES: | |
| if col not in df.columns: | |
| df[col] = "mixed" if col == "road_type" else "car" | |
| return df[ALL_FEATURES] | |