Skip to content

Commit

Permalink
ODSC-46457/refactor_forecast.csv (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
govarsha authored Sep 26, 2023
1 parent e106bd9 commit 02c1f00
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 14 deletions.
2 changes: 2 additions & 0 deletions ads/opctl/operator/lowcode/forecast/model/arima.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ def _build_model(self) -> pd.DataFrame:

output_i["Date"] = outputs[f"{col}_{cat}"].index
output_i["Series"] = cat
output_i["input_value"] = float("nan")
output_i[f"fitted_value"] = float("nan")
output_i[f"forecast_value"] = outputs[f"{col}_{cat}"]["yhat"].values
output_i[yhat_upper_name] = outputs[f"{col}_{cat}"]["yhat_upper"].values
output_i[yhat_lower_name] = outputs[f"{col}_{cat}"]["yhat_lower"].values
Expand Down
16 changes: 12 additions & 4 deletions ads/opctl/operator/lowcode/forecast/model/automlx.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,22 @@ def _build_model(self) -> pd.DataFrame:
# Merge the outputs from each model into 1 df with all outputs by target and category
col = self.original_target_column
output_col = pd.DataFrame()
yhat_lower_percentage = (
100 - (self.spec.confidence_interval_width or 0.5) * 100
) // 2
yhat_upper_name = "p" + str(int(100 - yhat_lower_percentage))
yhat_lower_name = "p" + str(int(yhat_lower_percentage))
for cat in self.categories: # Note: add [:2] to restrict
output_i = pd.DataFrame()
output_i[self.spec.datetime_column.name] = outputs[f"{col}_{cat}"]["ds"]
output_i["Date"] = outputs[f"{col}_{cat}"]["ds"]
output_i["Series"] = cat
output_i[f"{col}_forecast"] = outputs[f"{col}_{cat}"]["yhat"]
output_i[f"{col}_forecast_upper"] = outputs[f"{col}_{cat}"]["yhat_upper"]
output_i[f"{col}_forecast_lower"] = outputs[f"{col}_{cat}"]["yhat_lower"]
output_i["input_value"] = float("nan")
output_i[f"fitted_value"] = float("nan")
output_i[f"forecast_value"] = outputs[f"{col}_{cat}"]["yhat"]
output_i[yhat_upper_name] = outputs[f"{col}_{cat}"]["yhat_upper"]
output_i[yhat_lower_name] = outputs[f"{col}_{cat}"]["yhat_lower"]
output_col = pd.concat([output_col, output_i])

# output_col = output_col.sort_values(self.spec.datetime_column.name).reset_index(drop=True)
output_col = output_col.reset_index(drop=True)
outputs_merged = pd.concat([outputs_merged, output_col], axis=1)
Expand Down
46 changes: 39 additions & 7 deletions ads/opctl/operator/lowcode/forecast/model/neuralprophet.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,46 @@ def objective(trial):

output_i["Date"] = outputs[f"{col}_{cat}"]["ds"]
output_i["Series"] = cat
output_i[f"forecast_value"] = outputs[f"{col}_{cat}"]["yhat1"]
output_i[f"p{int(quantiles[1]*100)}"] = outputs[f"{col}_{cat}"][
f"yhat1 {quantiles[1]*100}%"
]
output_i[f"p{int(quantiles[0]*100)}"] = outputs[f"{col}_{cat}"][
f"yhat1 {quantiles[0]*100}%"
]
output_i[f"input_value"] = full_data_dict[f"{col}_{cat}"][f"{col}_{cat}"]

output_i[f"fitted_value"] = float("nan")
output_i[f"forecast_value"] = float("nan")
output_i[f"p{int(quantiles[1]*100)}"] = float("nan")
output_i[f"p{int(quantiles[0]*100)}"] = float("nan")

output_i.iloc[
: -self.spec.horizon.periods, output_i.columns.get_loc(f"fitted_value")
] = (
outputs[f"{col}_{cat}"]["yhat1"]
.iloc[: -self.spec.horizon.periods]
.values
)
output_i.iloc[
-self.spec.horizon.periods :,
output_i.columns.get_loc(f"forecast_value"),
] = (
outputs[f"{col}_{cat}"]["yhat1"]
.iloc[-self.spec.horizon.periods :]
.values
)
output_i.iloc[
-self.spec.horizon.periods :,
output_i.columns.get_loc(f"p{int(quantiles[1]*100)}"),
] = (
outputs[f"{col}_{cat}"][f"yhat1 {quantiles[1]*100}%"]
.iloc[-self.spec.horizon.periods :]
.values
)
output_i.iloc[
-self.spec.horizon.periods :,
output_i.columns.get_loc(f"p{int(quantiles[0]*100)}"),
] = (
outputs[f"{col}_{cat}"][f"yhat1 {quantiles[0]*100}%"]
.iloc[-self.spec.horizon.periods :]
.values
)
output_col = pd.concat([output_col, output_i])

# output_col = output_col.sort_values(operator.ds_column).reset_index(drop=True)
output_col = output_col.reset_index(drop=True)
outputs_merged = pd.concat([outputs_merged, output_col], axis=1)
Expand Down
40 changes: 37 additions & 3 deletions ads/opctl/operator/lowcode/forecast/model/prophet.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,44 @@ def objective(trial):

output_i["Date"] = outputs[f"{col}_{cat}"]["ds"]
output_i["Series"] = cat
output_i[f"forecast_value"] = outputs[f"{col}_{cat}"]["yhat"]
output_i[yhat_upper_name] = outputs[f"{col}_{cat}"]["yhat_upper"]
output_i[yhat_lower_name] = outputs[f"{col}_{cat}"]["yhat_lower"]
output_i["input_value"] = full_data_dict[f"{col}_{cat}"][f"{col}_{cat}"]

output_i[f"fitted_value"] = float("nan")
output_i[f"forecast_value"] = float("nan")
output_i[yhat_upper_name] = float("nan")
output_i[yhat_lower_name] = float("nan")

output_i.iloc[
: -self.spec.horizon.periods, output_i.columns.get_loc(f"fitted_value")
] = (
outputs[f"{col}_{cat}"]["yhat"]
.iloc[: -self.spec.horizon.periods]
.values
)
output_i.iloc[
-self.spec.horizon.periods :,
output_i.columns.get_loc(f"forecast_value"),
] = (
outputs[f"{col}_{cat}"]["yhat"]
.iloc[-self.spec.horizon.periods :]
.values
)
output_i.iloc[
-self.spec.horizon.periods :, output_i.columns.get_loc(yhat_upper_name)
] = (
outputs[f"{col}_{cat}"]["yhat_upper"]
.iloc[-self.spec.horizon.periods :]
.values
)
output_i.iloc[
-self.spec.horizon.periods :, output_i.columns.get_loc(yhat_lower_name)
] = (
outputs[f"{col}_{cat}"]["yhat_lower"]
.iloc[-self.spec.horizon.periods :]
.values
)
output_col = pd.concat([output_col, output_i])

# output_col = output_col.sort_values(self.spec.datetime_column.name).reset_index(drop=True)
output_col = output_col.reset_index(drop=True)
outputs_merged = pd.concat([outputs_merged, output_col], axis=1)
Expand Down

0 comments on commit 02c1f00

Please sign in to comment.