Hello, I’m learning to use `AutoMLForecast()` for ...
# mlforecast
q
Hello, I’m learning to use
AutoMLForecast()
for hyperparameter tuning and have set up my
init_config
. Is it possible to apply feature scaling, such as using `scikit-learn`'s
MinMaxScaler()
, on features created by
lag_transforms
in my
init_config
by incorporating a pipeline? If so, how can I do this? Any relevant documentation would be greatly appreciated. Thank you!
1
I think my challenge here is 1. I don't know how to extract feature names generated by lag_transforms to be used in the pipeline. 2. even if I manually typed feature names in the pipeline, it does not seem to work by passing the pipeline to "models" in AutoMLForecast
Ok, after several rounds of chatting with copilot, here is a solution that seems to work as expected
Copy code
class DynamicFeatureScaler(BaseEstimator, TransformerMixin):
    def __init__(self):
        self.feature_names_ = None
        self.scaler_ = MinMaxScaler()
        self.logger = logging.getLogger(__name__)
        
    def fit(self, X, y=None):
        self.feature_names_ = list(X.columns)
        self.scaler_.fit(X)
        <http://self.logger.info|self.logger.info>(f"Features scaled: {self.feature_names_}")
        return self
        
    def transform(self, X):
        return pd.DataFrame(
            self.scaler_.transform(X),
            columns=self.feature_names_,
            index=X.index
        )
    
    def get_feature_names(self):
        return self.feature_names_

logging.basicConfig(level=<http://logging.INFO|logging.INFO>)

# Create pipeline
feature_scaler = DynamicFeatureScaler()
ridge_pipe = Pipeline([
    ('scaler', feature_scaler),
    ('ridge_model', Ridge())
])
Copy code
auto_ridge = AutoModel(
  ridge_pipe, 
  lambda trial: {f'ridge_model__{k}': v for k, v in ridge_space(trial).items()}
)

auto_mlf = AutoMLForecast(
    models=[auto_ridge],
    freq='MS',
    init_config=my_init_config,
    fit_config=lambda trial: {'static_features': []}
)