major overhaul of forecast pipeline #21
@ -231,6 +231,8 @@ def _process_sales(
|
|||||||
"early_stopping_rounds": [20, 50],
|
"early_stopping_rounds": [20, 50],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# --- new: best_estimator
|
||||||
|
best_estimator: BestEstimatorXGBRegressor | None = None
|
||||||
best_params: BestParametersXGBRegressor | None = None
|
best_params: BestParametersXGBRegressor | None = None
|
||||||
best_score_mae: float | None = float("inf")
|
best_score_mae: float | None = float("inf")
|
||||||
best_score_r2: float | None = None
|
best_score_r2: float | None = None
|
||||||
@ -238,20 +240,29 @@ def _process_sales(
|
|||||||
too_few_month_points: bool = True
|
too_few_month_points: bool = True
|
||||||
forecast: pd.DataFrame | None = None
|
forecast: pd.DataFrame | None = None
|
||||||
|
|
||||||
# change sliding window to monthly basis
|
# --- new: dates und forecast
|
||||||
for start_year in range(current_year - 4, first_year - 1, -1):
|
#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(
|
train = cast(
|
||||||
pd.DataFrame,
|
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(
|
test = cast(
|
||||||
pd.DataFrame,
|
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]
|
X_train, X_test = train[features], test[features]
|
||||||
y_train, y_test = train[target], test[target]
|
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
|
too_few_month_points = False
|
||||||
|
|
||||||
rand = RandomizedSearchCV(
|
rand = RandomizedSearchCV(
|
||||||
@ -271,16 +282,19 @@ 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))
|
||||||
best_start_year = start_year
|
best_start_year = start_year
|
||||||
# overwrite with pre-defined prognosis DF
|
|
||||||
forecast = test.copy()
|
# --- new: use best_estimator to calculate future values and store them in forecast
|
||||||
forecast.loc[:, "vorhersage"] = y_pred
|
if best_estimator is not None:
|
||||||
# remove
|
X_future = pd.DataFrame({"jahr": future_dates.year, "monat": future_dates.month}, index=future_dates)
|
||||||
if forecast is not None:
|
y_future = rand.best_estimator_.predict(X_future)
|
||||||
forecast = forecast.drop(SALES_FEAT, axis=1).reset_index(drop=True)
|
forecast["vorhersage"] = y_future
|
||||||
|
|
||||||
best_score_mae = best_score_mae if not math.isinf(best_score_mae) else None
|
best_score_mae = best_score_mae if not math.isinf(best_score_mae) else None
|
||||||
|
|
||||||
if too_few_month_points:
|
if too_few_month_points:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user