-
Notifications
You must be signed in to change notification settings - Fork 875
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
SequentialFeatureSelection Early Stopping Criterion #886
base: master
Are you sure you want to change the base?
Changes from 4 commits
8bedc45
fff2163
f99fec4
4db4c97
d5595a9
c6e9ca8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,6 +114,11 @@ class SequentialFeatureSelector(_BaseXComposition, MetaEstimatorMixin): | |
n_jobs : int (default: 1) | ||
The number of CPUs to use for evaluating different feature subsets | ||
in parallel. -1 means 'all CPUs'. | ||
early_stop_rounds : int (default 0) | ||
Enable early stopping criterion when > 0, this value determines the | ||
number of iterations after which, if no performance boost has been | ||
seen, execution is stopped. | ||
Used only when `k_features == 'best'` or `k_features == 'parsimonious'` | ||
pre_dispatch : int, or string (default: '2*n_jobs') | ||
Controls the number of jobs that get dispatched | ||
during parallel execution if `n_jobs > 1` or `n_jobs=-1`. | ||
|
@@ -178,6 +183,7 @@ def __init__(self, estimator, k_features=1, | |
forward=True, floating=False, | ||
verbose=0, scoring=None, | ||
cv=5, n_jobs=1, | ||
early_stop_rounds=0, | ||
pre_dispatch='2*n_jobs', | ||
clone_estimator=True, | ||
fixed_features=None): | ||
|
@@ -201,6 +207,13 @@ def __init__(self, estimator, k_features=1, | |
self.verbose = verbose | ||
self.clone_estimator = clone_estimator | ||
|
||
if not isinstance(early_stop_rounds, int) or early_stop_rounds < 0: | ||
raise ValueError('Number of early stopping round should be ' | ||
'an integer value greater than or equal to 0.' | ||
'Got %d' % early_stop_rounds) | ||
|
||
self.early_stop_rounds = early_stop_rounds | ||
|
||
if fixed_features is not None: | ||
if isinstance(self.k_features, int) and \ | ||
self.k_features <= len(fixed_features): | ||
|
@@ -424,6 +437,8 @@ def fit(self, X, y, custom_feature_names=None, groups=None, **fit_params): | |
} | ||
best_subset = None | ||
k_score = 0 | ||
best_score = -np.inf | ||
early_stop_count = self.early_stop_rounds | ||
|
||
try: | ||
while k != k_to_select: | ||
|
@@ -550,6 +565,20 @@ def fit(self, X, y, custom_feature_names=None, groups=None, **fit_params): | |
X) | ||
raise KeyboardInterrupt | ||
|
||
# early stop | ||
if self.early_stop_rounds \ | ||
and k != k_to_select \ | ||
and self.k_features in {'best', 'parsimonious'}: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of the check here, i would maybe change it to raising a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rasbt Do you prefer having this check on top of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh, totally lost track and missed your comment! Sorry! Regarding your question, I think |
||
if k_score <= best_score: | ||
early_stop_count -= 1 | ||
if early_stop_count == 0: | ||
print('Performances not improved for %d rounds. ' | ||
'Stopping now!' % self.early_stop_rounds) | ||
break | ||
else: | ||
early_stop_count = self.early_stop_rounds | ||
best_score = k_score | ||
|
||
except KeyboardInterrupt: | ||
self.interrupted_ = True | ||
sys.stderr.write('\nSTOPPING EARLY DUE TO KEYBOARD INTERRUPT...') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
...Got %d' % early_stop_rounds
might not work ifearly_stop_rounds
is not an integer. Maybe it's better to replace it with...Got %s' % early_stop_rounds
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right!