Spaces:
Running
Running
Update feature_handler.py
Browse files- feature_handler.py +3 -8
feature_handler.py
CHANGED
|
@@ -3,9 +3,6 @@ import numpy as np
|
|
| 3 |
from config import CITIES_COORDS
|
| 4 |
|
| 5 |
def apply_feature_engineering(df_input):
|
| 6 |
-
"""
|
| 7 |
-
Version Experte : Sécurise les calculs temporels par ville et nettoie les types de données.
|
| 8 |
-
"""
|
| 9 |
df = df_input.copy()
|
| 10 |
|
| 11 |
# 1. Nettoyage des types (Évite l'erreur TypeError str vs int)
|
|
@@ -69,8 +66,9 @@ def apply_feature_engineering(df_input):
|
|
| 69 |
df['temp_anomaly'] = df['temperature_2m_mean'] - df['temp_roll30']
|
| 70 |
df['precip_cumul7'] = grouped['precipitation_sum'].transform(lambda x: x.rolling(7, min_periods=1).sum())
|
| 71 |
df['precip_cumul3'] = grouped['precipitation_sum'].transform(lambda x: x.rolling(3, min_periods=1).sum())
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
def categorize_weather(code):
|
| 75 |
if code == 0: return 'Ciel dégagé'
|
| 76 |
elif code in [1, 2]: return 'Nuageux'
|
|
@@ -79,9 +77,6 @@ def apply_feature_engineering(df_input):
|
|
| 79 |
|
| 80 |
df['weather_categorie'] = df['weather_code'].apply(categorize_weather)
|
| 81 |
df['weather_encoded'] = df['weather_code'].astype(float)
|
| 82 |
-
|
| 83 |
-
# 7. Nettoyage final des NaNs générés par les shifts (Lags) au sein de chaque ville
|
| 84 |
-
# On remplit les trous par les valeurs suivantes/précédentes de la MÊME ville
|
| 85 |
cols_to_fix = [c for c in df.columns if 'lag' in c or 'roll' in c]
|
| 86 |
for col in cols_to_fix:
|
| 87 |
df[col] = df.groupby('city')[col].ffill().bfill()
|
|
|
|
| 3 |
from config import CITIES_COORDS
|
| 4 |
|
| 5 |
def apply_feature_engineering(df_input):
|
|
|
|
|
|
|
|
|
|
| 6 |
df = df_input.copy()
|
| 7 |
|
| 8 |
# 1. Nettoyage des types (Évite l'erreur TypeError str vs int)
|
|
|
|
| 66 |
df['temp_anomaly'] = df['temperature_2m_mean'] - df['temp_roll30']
|
| 67 |
df['precip_cumul7'] = grouped['precipitation_sum'].transform(lambda x: x.rolling(7, min_periods=1).sum())
|
| 68 |
df['precip_cumul3'] = grouped['precipitation_sum'].transform(lambda x: x.rolling(3, min_periods=1).sum())
|
| 69 |
+
def get_region_name(city):
|
| 70 |
+
return CITIES_COORDS.get(city, {}).get('region', 'Littoral')
|
| 71 |
+
df['region'] = df['city'].apply(get_region_name)
|
| 72 |
def categorize_weather(code):
|
| 73 |
if code == 0: return 'Ciel dégagé'
|
| 74 |
elif code in [1, 2]: return 'Nuageux'
|
|
|
|
| 77 |
|
| 78 |
df['weather_categorie'] = df['weather_code'].apply(categorize_weather)
|
| 79 |
df['weather_encoded'] = df['weather_code'].astype(float)
|
|
|
|
|
|
|
|
|
|
| 80 |
cols_to_fix = [c for c in df.columns if 'lag' in c or 'roll' in c]
|
| 81 |
for col in cols_to_fix:
|
| 82 |
df[col] = df.groupby('city')[col].ffill().bfill()
|