Skip to content

max_features being ignored in feature selection pipeline #39

Description

@thomasATbayer

Problem

Feature Selection Pipeline max_features parameter is ignored when building a pipeline with HyperparameterRooting.

the configured cap is not effectively enforced at runtime.

Root Cause (Hypothesis)

MotherSelectFromModel receives max_features through **kwargs instead of an explicit __init__ parameter.

sklearn pipelines clone estimators internally during fit using:

  1. get_params(deep=False)
  2. Reconstruct estimator from returned params

BaseEstimator.get_params() only inspects the current class __init__ signature. Because max_features is not explicitly declared in MotherSelectFromModel.__init__, it is missing from get_params().

As a result, the cloned selector is rebuilt with max_features=None, so the cap is silently dropped.

Suggested Fix

Add max_features explicitly to MotherSelectFromModel.__init__ (instead of only passing via **kwargs) in file ml/models/estimators.py:

class MotherSelectFromModel(SelectFromModel, ml.AbstractMotherPipeline):
  def __init__(
    self,
    estimator,
    threshold=np.finfo(np.float64).tiny,
    max_features=None, # explicit argument
    **kwargs,
  ):
    super().__init__(
      estimator=estimator,
      threshold=threshold,
      max_features=max_features,
      **kwargs,
    )

This keeps max_features visible to get_params(), so sklearn clone() preserves it.

Working Workaround

By using a thin local wrapper that explicitly exposes max_features in the __init__ the problem is solved.

class _SelectFromModelWithMaxFeatures(MotherSelectFromModel):
    def __init__(self, estimator, threshold=np.finfo(np.float64).tiny, max_features=None, **kwargs):
        super().__init__(
            estimator=estimator,
            threshold=threshold,
            max_features=max_features,
            **kwargs,
        )

Then instantiate this wrapper instead of MotherSelectFromModel in _create_feature_selection_pipeline.

Metadata

Metadata

Labels

bugSomething isn't workingenhancementNew feature or request

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions