Skip to content
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

Fix #553 #554

Merged
merged 4 commits into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions doc/whats_new/v0.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ Bug
- Fix bug which converting to COO format sparse when stacking the matrices in
:class:`imblearn.over_sampling.SMOTENC`. This bug was only old scipy version.
:issue:`539` by :user:`Guillaume Lemaitre <glemaitre>`.

- Fix bug in :class:`imblearn.pipeline.Pipeline` where None could be the final
estimator.
:issue:`554` by :user:`Oliver Rausch <orausch>`.
8 changes: 7 additions & 1 deletion imblearn/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ def fit_transform(self, X, y=None, **fit_params):
else:
return last_step.fit(Xt, yt, **fit_params).transform(Xt)

@if_delegate_has_method(delegate='_final_estimator')
def fit_resample(self, X, y=None, **fit_params):
"""Fit the model and sample with the final estimator

Expand Down Expand Up @@ -525,6 +524,13 @@ def _inverse_transform(self, X):
Xt = transform.inverse_transform(Xt)
return Xt

# need to overwrite sklearn's _final_estimator since sklearn supports
# 'passthrough', but imblearn does not.
@property
def _final_estimator(self):
estimator = self.steps[-1][1]
return estimator

@if_delegate_has_method(delegate='_final_estimator')
def score(self, X, y=None, sample_weight=None):
"""Apply transformers/samplers, and score with the final estimator
Expand Down
19 changes: 19 additions & 0 deletions imblearn/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,3 +1104,22 @@ def test_predict_with_predict_params():
pipe.fit(None, None)
pipe.predict(X=None, got_attribute=True)
assert pipe.named_steps['clf'].got_attribute


def test_resampler_last_stage_passthrough():

X, y = make_classification(
n_classes=2,
class_sep=2,
weights=[0.1, 0.9],
n_informative=3,
n_redundant=1,
flip_y=0,
n_features=20,
n_clusters_per_class=1,
n_samples=50000,
random_state=0)

rus = RandomUnderSampler(random_state=42)
pipe = make_pipeline(rus, None)
pipe.fit_resample(X, y)