https://github.com/nixtla logo
#general
Title
# general
e

Evan Miller

09/06/2023, 10:31 PM
Another question about MLForecast: Is it possible to train different models on different subsets features, without having to make separate MLForecast objects? I'd like to be able to do the preprocessing only once, but then train multiple models on different sets of features. I can run the preprocessing and train the models easily enough, but when I go to predict them using the MLForecast.ts.predict() method I get an error like: ValueError: Number of features of the model must match the input. Model n_features_ is 31 and input n_features is 32 Is it possible to manipulate the data in the MLForecast object to only select the features used in training?
j

José Morales

09/06/2023, 10:42 PM
Hey. I think you could run MLorecast.ts.predict passing a function that selects the features your model uses through before_predict_callback, something like:
Copy code
from functools import partial

def select_features(df, features):
    return df[features]

features_model1 = partial(select_features, features=['x1, 'x2'])
features_model2 = partial(select_features, features=['x2', 'x3'])
predictions = []
for model, callback in zip([model1, model2], [features_model1, features_model2]):
    model_preds = fcst.ts.predict({'model': model}, before_predict_callback=callback)
    predictions.append(model_preds)
e

Evan Miller

09/06/2023, 11:04 PM
yep this seems to work! Thank you!