major overhaul of forecast pipeline #21

Merged
foefl merged 15 commits from prediction_to_future into main 2025-04-16 09:24:34 +00:00
Showing only changes of commit 4ef8fc5e9d - Show all commits

View File

@ -231,6 +231,8 @@ def _process_sales(
"early_stopping_rounds": [20, 50],
}
# --- new: best_estimator
best_estimator: BestEstimatorXGBRegressor | None = None
best_params: BestParametersXGBRegressor | None = None
best_score_mae: float | None = float("inf")
best_score_r2: float | None = None
@ -238,20 +240,29 @@ def _process_sales(
too_few_month_points: bool = True
forecast: pd.DataFrame | None = None
# change sliding window to monthly basis
for start_year in range(current_year - 4, first_year - 1, -1):
# --- new: dates und forecast
#last_date = pd.to_datetime(monthly_sum.index[-1], format="%m.%Y")
last_date = pd.to_datetime(datetime.now().strftime("%m.%Y"), format="%m.%Y")
future_dates = pd.date_range(start=last_date + pd.DateOffset(months=1), periods=anzahl, freq="MS")
forecast = pd.DataFrame({"Datum": future_dates.strftime("%m.%Y")}).set_index("Datum")
dates = monthly_sum.index
for index, i in enumerate(range(len(dates)-36, -1, -12)):
current_date = dates[i]
split_date = dates[-anzahl]
train = cast(
pd.DataFrame,
monthly_sum[monthly_sum.index.year >= start_year].iloc[:-5].copy(), # type: ignore
monthly_sum.loc[current_date:split_date].copy(), # type: ignore
)
test = cast(
pd.DataFrame,
monthly_sum[monthly_sum.index.year >= start_year].iloc[-5:].copy(), # type: ignore
monthly_sum.loc[split_date:].copy(), # type: ignore
)
X_train, X_test = train[features], test[features]
y_train, y_test = train[target], test[target]
if len(train) >= (base_num_data_points_months + 10 * (current_year - 4 - start_year)):
if len(train) >= 30 + 10 * index:
too_few_month_points = False
rand = RandomizedSearchCV(
@ -271,16 +282,19 @@ def _process_sales(
if len(np.unique(y_pred)) != 1:
error = cast(float, mean_absolute_error(y_test, y_pred))
if error < best_score_mae:
# --- new: store best_estimator
best_estimator = cast(BestEstimatorXGBRegressor, rand.best_estimator_)
best_params = cast(BestParametersXGBRegressor, rand.best_params_)
best_score_mae = error
best_score_r2 = cast(float, r2_score(y_test, y_pred))
best_start_year = start_year
# overwrite with pre-defined prognosis DF
forecast = test.copy()
forecast.loc[:, "vorhersage"] = y_pred
# remove
if forecast is not None:
forecast = forecast.drop(SALES_FEAT, axis=1).reset_index(drop=True)
# --- new: use best_estimator to calculate future values and store them in forecast
if best_estimator is not None:
X_future = pd.DataFrame({"jahr": future_dates.year, "monat": future_dates.month}, index=future_dates)
y_future = rand.best_estimator_.predict(X_future)
forecast["vorhersage"] = y_future
best_score_mae = best_score_mae if not math.isinf(best_score_mae) else None
if too_few_month_points: