Skip to content

Feature range parameter support #1044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions mlxtend/feature_selection/exhaustive_feature_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ class ExhaustiveFeatureSelector(BaseEstimator, MetaEstimatorMixin):
def __init__(
self,
estimator,
min_features=1,
max_features=1,
min_features=None,
max_features=None,
print_progress=True,
scoring="accuracy",
cv=5,
Expand All @@ -179,11 +179,13 @@ def __init__(
clone_estimator=True,
fixed_features=None,
feature_groups=None,
feature_range = None
):
self.estimator = estimator
self.min_features = min_features
self.max_features = max_features
self.pre_dispatch = pre_dispatch
self.feature_range = feature_range
# Want to raise meaningful error message if a
# cross-validation generator is inputted
if isinstance(cv, types.GeneratorType):
Expand Down Expand Up @@ -401,8 +403,26 @@ def fit(self, X, y, groups=None, **fit_params):

# candidates in the following lines are the non-fixed-features candidates
# (the fixed features will be added later to each combination)
min_num_candidates = self.min_features - len(self.fixed_features_group_set)
max_num_candidates = self.max_features - len(self.fixed_features_group_set)

if self.min_features == None and self.max_features == None:
if self.feature_range == None:
min_num_candidates = 1
max_num_candidates = 1
elif self.feature_range == "all":
min_num_candidates = 1
max_num_candidates = X.shape[1]
else:
try:
min_num_candidates = self.feature_range[0]
max_num_candidates = self.feature_range[1]
except ValueError:
raise ValueError(
"""feature_range should be of tuple type. First argument should be
minimum number of feature and second argument shoulf be maximum number of feature"""
)
else:
min_num_candidates = self.min_features - len(self.fixed_features_group_set)
max_num_candidates = self.max_features - len(self.fixed_features_group_set)
candidates = chain.from_iterable(
combinations(non_fixed_groups, r=i)
for i in range(min_num_candidates, max_num_candidates + 1)
Expand Down