|
| 1 | +import pandas as pd |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +from ads.opctl import logger |
| 5 | +from ads.common.decorator import runtime_dependency |
| 6 | +from .base_model import ForecastOperatorBaseModel |
| 7 | +from .forecast_datasets import ForecastDatasets, ForecastOutput |
| 8 | +from ..operator_config import ForecastOperatorConfig |
| 9 | +from ..const import ForecastOutputColumns, SupportedModels |
| 10 | + |
| 11 | + |
| 12 | +class MLForecastOperatorModel(ForecastOperatorBaseModel): |
| 13 | + """Class representing MLForecast operator model.""" |
| 14 | + |
| 15 | + def __init__(self, config: ForecastOperatorConfig, datasets: ForecastDatasets): |
| 16 | + super().__init__(config=config, datasets=datasets) |
| 17 | + self.global_explanation = {} |
| 18 | + self.local_explanation = {} |
| 19 | + self.formatted_global_explanation = None |
| 20 | + self.formatted_local_explanation = None |
| 21 | + |
| 22 | + def set_kwargs(self): |
| 23 | + """ |
| 24 | + Returns the model parameters. |
| 25 | + """ |
| 26 | + model_kwargs = self.spec.model_kwargs |
| 27 | + |
| 28 | + uppper_quantile = round(0.5 + self.spec.confidence_interval_width / 2, 2) |
| 29 | + lower_quantile = round(0.5 - self.spec.confidence_interval_width / 2, 2) |
| 30 | + |
| 31 | + model_kwargs["lower_quantile"] = lower_quantile |
| 32 | + model_kwargs["uppper_quantile"] = uppper_quantile |
| 33 | + return model_kwargs |
| 34 | + |
| 35 | + def preprocess(self, df, series_id): |
| 36 | + pass |
| 37 | + |
| 38 | + @runtime_dependency( |
| 39 | + module="mlforecast", |
| 40 | + err_msg="MLForecast is not installed, please install it with 'pip install mlforecast'", |
| 41 | + ) |
| 42 | + @runtime_dependency( |
| 43 | + module="lightgbm", |
| 44 | + err_msg="lightgbm is not installed, please install it with 'pip install lightgbm'", |
| 45 | + ) |
| 46 | + def _train_model(self, data_train, data_test, model_kwargs): |
| 47 | + try: |
| 48 | + |
| 49 | + import lightgbm as lgb |
| 50 | + from mlforecast import MLForecast |
| 51 | + from mlforecast.lag_transforms import ExpandingMean, RollingMean |
| 52 | + from mlforecast.target_transforms import Differences |
| 53 | + |
| 54 | + lgb_params = { |
| 55 | + "verbosity": -1, |
| 56 | + "num_leaves": 512, |
| 57 | + } |
| 58 | + |
| 59 | + fcst = MLForecast( |
| 60 | + models={ |
| 61 | + "forecast": lgb.LGBMRegressor(**lgb_params), |
| 62 | + # "p" + str(int(model_kwargs["uppper_quantile"] * 100)) |
| 63 | + "upper": lgb.LGBMRegressor( |
| 64 | + **lgb_params, |
| 65 | + objective="quantile", |
| 66 | + alpha=model_kwargs["uppper_quantile"], |
| 67 | + ), |
| 68 | + # "p" + str(int(model_kwargs["lower_quantile"] * 100)) |
| 69 | + "lower": lgb.LGBMRegressor( |
| 70 | + **lgb_params, |
| 71 | + objective="quantile", |
| 72 | + alpha=model_kwargs["lower_quantile"], |
| 73 | + ), |
| 74 | + }, |
| 75 | + freq=pd.infer_freq(data_train.Date.drop_duplicates()), |
| 76 | + target_transforms=[Differences([12])], |
| 77 | + lags=model_kwargs.get("lags", [1, 6, 12]), |
| 78 | + lag_transforms={ |
| 79 | + 1: [ExpandingMean()], |
| 80 | + 12: [RollingMean(window_size=24)], |
| 81 | + }, |
| 82 | + # date_features=[hour_index], |
| 83 | + ) |
| 84 | + |
| 85 | + num_models = model_kwargs.get("recursive_models", False) |
| 86 | + |
| 87 | + fcst.fit( |
| 88 | + data_train, |
| 89 | + static_features=model_kwargs.get("static_features", []), |
| 90 | + id_col=ForecastOutputColumns.SERIES, |
| 91 | + time_col=self.spec.datetime_column.name, |
| 92 | + target_col=self.spec.target_column, |
| 93 | + fitted=True, |
| 94 | + max_horizon=None if num_models is False else self.spec.horizon, |
| 95 | + ) |
| 96 | + |
| 97 | + self.outputs = fcst.predict( |
| 98 | + h=self.spec.horizon, |
| 99 | + X_df=pd.concat( |
| 100 | + [ |
| 101 | + data_test, |
| 102 | + fcst.get_missing_future(h=self.spec.horizon, X_df=data_test), |
| 103 | + ], |
| 104 | + axis=0, |
| 105 | + ignore_index=True, |
| 106 | + ).fillna(0), |
| 107 | + ) |
| 108 | + fitted_values = fcst.forecast_fitted_values() |
| 109 | + for s_id in self.datasets.list_series_ids(): |
| 110 | + self.forecast_output.init_series_output( |
| 111 | + series_id=s_id, |
| 112 | + data_at_series=self.datasets.get_data_at_series(s_id), |
| 113 | + ) |
| 114 | + |
| 115 | + self.forecast_output.populate_series_output( |
| 116 | + series_id=s_id, |
| 117 | + fit_val=fitted_values[ |
| 118 | + fitted_values[ForecastOutputColumns.SERIES] == s_id |
| 119 | + ].forecast.values, |
| 120 | + forecast_val=self.outputs[ |
| 121 | + self.outputs[ForecastOutputColumns.SERIES] == s_id |
| 122 | + ].forecast.values, |
| 123 | + upper_bound=self.outputs[ |
| 124 | + self.outputs[ForecastOutputColumns.SERIES] == s_id |
| 125 | + ].upper.values, |
| 126 | + lower_bound=self.outputs[ |
| 127 | + self.outputs[ForecastOutputColumns.SERIES] == s_id |
| 128 | + ].lower.values, |
| 129 | + ) |
| 130 | + |
| 131 | + self.model_parameters[s_id] = { |
| 132 | + "framework": SupportedModels.MLForecast, |
| 133 | + **lgb_params, |
| 134 | + } |
| 135 | + |
| 136 | + logger.debug("===========Done===========") |
| 137 | + |
| 138 | + return self.forecast_output.get_forecast_long() |
| 139 | + except Exception as e: |
| 140 | + self.errors_dict[self.spec.model] = { |
| 141 | + "model_name": self.spec.model, |
| 142 | + "error": str(e), |
| 143 | + } |
| 144 | + |
| 145 | + def _build_model(self) -> pd.DataFrame: |
| 146 | + data_train = self.datasets.get_all_data_long(include_horizon=False) |
| 147 | + data_test = self.datasets.get_all_data_long_test() |
| 148 | + self.models = dict() |
| 149 | + model_kwargs = self.set_kwargs() |
| 150 | + self.forecast_output = ForecastOutput( |
| 151 | + confidence_interval_width=self.spec.confidence_interval_width, |
| 152 | + horizon=self.spec.horizon, |
| 153 | + target_column=self.original_target_column, |
| 154 | + dt_column=self.spec.datetime_column.name, |
| 155 | + ) |
| 156 | + self._train_model(data_train, data_test, model_kwargs) |
| 157 | + pass |
| 158 | + |
| 159 | + def _generate_report(self): |
| 160 | + pass |
0 commit comments