I have a question related to the max_horizon param...
# mlforecast
d
I have a question related to the max_horizon params. First question to see if I understood it right: • when we apply the max_horizon param during the preprocessing it expands the target to be represented has a multi-target prediction correct? If the above is true, then it fits one model per each target, or does it fit one model for multi target, and then propagates that model? the fit_models method is a little unclear for me. thank you
j
yes
d
Sorry, send it without finishing it... 😅
j
It fits one model to each target
d
the key part I am trying to act, is since I am forecasting with max_horizon=6, and I am trying to leverage the finetune by gridsearching better params for each horizon individually, I am currently fitting 21 model to end up with six... h1 = 1 model h2 = 2 models ... h6 = 6 models Where then I just replace in the last list, the with the other models previously fitted. But this is massive on our computer needs, and I am trying bypass that, by fitting the model with the exact params for each horizon once. Basically I am trying to catch the fit iteration to overwrite the params after each horizon
I mean before it starts fitting for that horizon
j
The
models_
attribute for the direct approach is a dict from str to a list of models (one for each horizon), so you can override that with whatever you want as long as it matches that structure. So you could generate the targets, do hyperparameter optimization for each horizon and then set each model to its corresponding position, e.g.
Copy code
X, ys = mlf.preprocess(data, max_horizon=6, return_X_y=True)
models = []
for y in ys:
    mask = ~np.isnan(y)
    best_model = HParamSearch.fit(X[mask], y[mask])
    models.append(best_model)
mlf.models_ = {'my_model': models}
mlf.predict(...)
d
I ended up initializing the MLForecast(models=list_of_models) where that list_of_models is the same model with different params for each horizon then I updated the fit_models:
Copy code
self.models_: Dict[str, Union[BaseEstimator, List[BaseEstimator]]] = {}
        if y.ndim == 2 and y.shape[1] > 1:

############################################
            for col, (name, model) in zip(range(y.shape[1]), self.models.items()):
###############################################3
                self.models_[name] = []
                keep = ~np.isnan(y[:, col])
                if isinstance(X, np.ndarray):
                    Xh = X[keep]
                else:
                    Xh = ufp.filter_with_mask(X, keep)
                yh = y[keep, col]
                self.models_[name].append(clone(model).fit(Xh, yh))
        else:
            for name, model in self.models.items():
                self.models_[name] = clone(model).fit(X, y)
        return self
zipping the loop also ensures that task... just need to ensure the number of models is equal to the max_horizon