From f49744ca45de600123123db581e12c48c1f60283 Mon Sep 17 00:00:00 2001 From: frasu Date: Thu, 10 Apr 2025 17:33:00 +0000 Subject: [PATCH] src/delta_barth/analysis/forecast.py aktualisiert --- src/delta_barth/analysis/forecast.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/delta_barth/analysis/forecast.py b/src/delta_barth/analysis/forecast.py index c32e078..bc402bc 100644 --- a/src/delta_barth/analysis/forecast.py +++ b/src/delta_barth/analysis/forecast.py @@ -218,12 +218,7 @@ def _process_sales( features = ["jahr", "monat"] target = SALES_FEAT - # --- new: not necessary anymore - #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") + # --- new: dates and forecast 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") forecast = pd.DataFrame({"datum": future_dates.strftime("%m.%Y")}).set_index("datum") @@ -241,7 +236,7 @@ def _process_sales( "early_stopping_rounds": [20, 50], } - # --- new: best_estimator (internal usage) + # --- new: best_estimator (internal usage only) best_estimator = 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) for index, i in enumerate(range(start_index, -1, -12)): - start_date = dates[i] + first_date = dates[i] split_date = dates[-6] train = cast( pd.DataFrame, - monthly_sum.loc[start_date:split_date].copy(), # type: ignore + monthly_sum.loc[first_date:split_date].copy(), # type: ignore ) test = cast( pd.DataFrame, @@ -272,6 +267,7 @@ def _process_sales( X_train, X_test = train[features], test[features] y_train, y_test = train[target], test[target] + # --- new: adapted condition to fit new for-loop if len(train) >= 30 + 10 * index: too_few_month_points = False @@ -292,13 +288,13 @@ 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)) - # --- new: use store start_date in best_start_year - best_start_year = start_date + # --- new: use first_date for best_start_year + 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 if best_estimator is not None: