Add improvements
This commit is contained in:
@@ -195,7 +195,46 @@ class PredictionService:
|
||||
|
||||
# Prepare features for Prophet model
|
||||
prophet_df = self._prepare_prophet_features(features)
|
||||
|
||||
|
||||
# CRITICAL FIX: Validate that model's required regressors are present
|
||||
# Warn if using default values for features the model was trained with
|
||||
if hasattr(model, 'extra_regressors'):
|
||||
model_regressors = set(model.extra_regressors.keys()) if model.extra_regressors else set()
|
||||
provided_features = set(prophet_df.columns) - {'ds'}
|
||||
|
||||
# Check for missing regressors
|
||||
missing_regressors = model_regressors - provided_features
|
||||
|
||||
if missing_regressors:
|
||||
logger.warning(
|
||||
"Model trained with regressors that are missing in prediction",
|
||||
model_id=model_id,
|
||||
missing_regressors=list(missing_regressors)[:10], # Log first 10
|
||||
total_missing=len(missing_regressors)
|
||||
)
|
||||
|
||||
# Check for default-valued critical features
|
||||
critical_features = {
|
||||
'traffic_volume', 'temperature', 'precipitation',
|
||||
'lag_1_day', 'rolling_mean_7d'
|
||||
}
|
||||
using_defaults = []
|
||||
for feature in critical_features:
|
||||
if feature in model_regressors:
|
||||
value = features.get(feature, 0)
|
||||
# Check if using default/fallback values
|
||||
if (feature == 'traffic_volume' and value == 100.0) or \
|
||||
(feature == 'temperature' and value == 15.0) or \
|
||||
(feature in ['lag_1_day', 'rolling_mean_7d'] and value == 0.0):
|
||||
using_defaults.append(feature)
|
||||
|
||||
if using_defaults:
|
||||
logger.warning(
|
||||
"Using default values for critical model features",
|
||||
model_id=model_id,
|
||||
features_with_defaults=using_defaults
|
||||
)
|
||||
|
||||
# Generate prediction
|
||||
forecast = model.predict(prophet_df)
|
||||
|
||||
@@ -938,8 +977,9 @@ class PredictionService:
|
||||
'is_month_end': int(forecast_date.day >= 28),
|
||||
'is_payday_period': int((forecast_date.day <= 5) or (forecast_date.day >= 25)),
|
||||
# CRITICAL FIX: Add is_payday feature to match training service
|
||||
# Training defines: is_payday = (day == 15 OR is_month_end)
|
||||
'is_payday': int((forecast_date.day == 15) or self._is_end_of_month(forecast_date)),
|
||||
# Training defines: is_payday = (day == 15 OR day == 28 OR is_month_end)
|
||||
# Spain commonly pays on 28th, 15th, or last day of month
|
||||
'is_payday': int((forecast_date.day == 15) or (forecast_date.day == 28) or self._is_end_of_month(forecast_date)),
|
||||
|
||||
# Weather-based derived features
|
||||
'temp_squared': temperature ** 2,
|
||||
|
||||
Reference in New Issue
Block a user