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 f49744ca45 - Show all commits

View File

@ -218,12 +218,7 @@ def _process_sales(
features = ["jahr", "monat"] features = ["jahr", "monat"]
target = SALES_FEAT target = SALES_FEAT
# --- new: not necessary anymore # --- new: dates and forecast
#current_year = datetime.datetime.now().year
#first_year = cast(int, df_cust["jahr"].min())
# --- 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") 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=6, freq="MS") future_dates = pd.date_range(start=last_date + pd.DateOffset(months=1), periods=6, freq="MS")
forecast = pd.DataFrame({"datum": future_dates.strftime("%m.%Y")}).set_index("datum") forecast = pd.DataFrame({"datum": future_dates.strftime("%m.%Y")}).set_index("datum")
@ -241,7 +236,7 @@ def _process_sales(
"early_stopping_rounds": [20, 50], "early_stopping_rounds": [20, 50],
} }
# --- new: best_estimator (internal usage) # --- new: best_estimator (internal usage only)
best_estimator = None best_estimator = None
best_params: BestParametersXGBRegressor | None = None best_params: BestParametersXGBRegressor | None = None
@ -258,12 +253,12 @@ def _process_sales(
start_index = next((i for i, date in enumerate(dates) if date >= starting_date), len(dates) - 1) start_index = next((i for i, date in enumerate(dates) if date >= starting_date), len(dates) - 1)
for index, i in enumerate(range(start_index, -1, -12)): for index, i in enumerate(range(start_index, -1, -12)):
start_date = dates[i] first_date = dates[i]
split_date = dates[-6] split_date = dates[-6]
train = cast( train = cast(
pd.DataFrame, pd.DataFrame,
monthly_sum.loc[start_date:split_date].copy(), # type: ignore monthly_sum.loc[first_date:split_date].copy(), # type: ignore
) )
test = cast( test = cast(
pd.DataFrame, pd.DataFrame,
@ -272,6 +267,7 @@ def _process_sales(
X_train, X_test = train[features], test[features] X_train, X_test = train[features], test[features]
y_train, y_test = train[target], test[target] y_train, y_test = train[target], test[target]
# --- new: adapted condition to fit new for-loop
if len(train) >= 30 + 10 * index: if len(train) >= 30 + 10 * index:
too_few_month_points = False too_few_month_points = False
@ -292,13 +288,13 @@ def _process_sales(
if len(np.unique(y_pred)) != 1: if len(np.unique(y_pred)) != 1:
error = cast(float, mean_absolute_error(y_test, y_pred)) error = cast(float, mean_absolute_error(y_test, y_pred))
if error < best_score_mae: if error < best_score_mae:
# --- new: store best_estimator
best_estimator = cast(BestEstimatorXGBRegressor, rand.best_estimator_)
best_params = cast(BestParametersXGBRegressor, rand.best_params_) best_params = cast(BestParametersXGBRegressor, rand.best_params_)
best_score_mae = error best_score_mae = error
best_score_r2 = cast(float, r2_score(y_test, y_pred)) best_score_r2 = cast(float, r2_score(y_test, y_pred))
# --- new: use store start_date in best_start_year # --- new: use first_date for best_start_year
best_start_year = start_date best_start_year = first_date.year
# --- new: store best_estimator
best_estimator = rand.best_estimator_
# --- new: use best_estimator to calculate future values and store them in forecast # --- new: use best_estimator to calculate future values and store them in forecast
if best_estimator is not None: if best_estimator is not None: