From 4ef8fc5e9d54b2296306b5abf9bd6ef37de408df Mon Sep 17 00:00:00 2001 From: frasu Date: Thu, 10 Apr 2025 14:58:01 +0000 Subject: [PATCH] src/delta_barth/analysis/forecast.py aktualisiert --- src/delta_barth/analysis/forecast.py | 36 +++++++++++++++++++--------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/delta_barth/analysis/forecast.py b/src/delta_barth/analysis/forecast.py index 60b619c..5505613 100644 --- a/src/delta_barth/analysis/forecast.py +++ b/src/delta_barth/analysis/forecast.py @@ -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: