Skip to content

SelectFromModel: max_features can't be greater than number of features #21117

@kienerj

Description

@kienerj

Describe the bug

When I define a SelectFromModel instance like here:

SelectFromModel(RandomForestClassifier(), max_features=100)

and the number of total features is less than 100, then a ValueError is raised:

ValueError: 'max_features' should be 0 and 10 features.Got 100 instead.

I consider this a bug as for example in my pipeline this feature selector is preceded by other feature selectors like low VarianceThreshold. Point being it is never known how many features will be left when this point is reached. If the value is bigger than available features it should just keep all of them and not throw an error.

Steps/Code to Reproduce

from sklearn.datasets import make_classification
from sklearn.feature_selection import SelectFromModel
from sklearn.ensemble import RandomForestClassifier

x,y = make_classification(n_features=10, n_informative=8)
sfm = SelectFromModel(RandomForestClassifier(), max_features=100)
sfm.fit(x,y)

Expected Results

If the value is bigger than available features it should just keep all of them and not throw an error.

Actual Results

ValueError: 'max_features' should be 0 and 10 features.Got 100 instead.

Versions

0.23.2
0.24.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions