Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

API Adds predict_params for Pipeline proba delegates #19790

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

Merged
merged 2 commits into from
Apr 5, 2021
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
8 changes: 8 additions & 0 deletions 8 doc/whats_new/v1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ Changelog
Use ``var_`` instead.
:pr:`18842` by :user:`Hong Shao Yang <hongshaoyang>`.

:mod:`sklearn.pipeline`
.......................

- |API| The `predict_proba` and `predict_log_proba` methods of the
:class:`Pipeline` class now support passing prediction kwargs to
the final estimator.
:pr:`19790` by :user:`Christopher Flynn <crflynn>`.

:mod:`sklearn.preprocessing`
............................

Expand Down
18 changes: 14 additions & 4 deletions 18 sklearn/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ def fit_predict(self, X, y=None, **fit_params):
return y_pred

@if_delegate_has_method(delegate='_final_estimator')
def predict_proba(self, X):
def predict_proba(self, X, **predict_proba_params):
"""Apply transforms, and predict_proba of the final estimator

Parameters
Expand All @@ -465,14 +465,18 @@ def predict_proba(self, X):
Data to predict on. Must fulfill input requirements of first step
of the pipeline.

**predict_proba_params : dict of string -> object
Parameters to the ``predict_proba`` called at the end of all
transformations in the pipeline.

Returns
-------
y_proba : array-like of shape (n_samples, n_classes)
"""
Xt = X
for _, name, transform in self._iter(with_final=False):
Xt = transform.transform(Xt)
return self.steps[-1][-1].predict_proba(Xt)
return self.steps[-1][-1].predict_proba(Xt, **predict_proba_params)

@if_delegate_has_method(delegate='_final_estimator')
def decision_function(self, X):
Expand Down Expand Up @@ -513,7 +517,7 @@ def score_samples(self, X):
return self.steps[-1][-1].score_samples(Xt)

@if_delegate_has_method(delegate='_final_estimator')
def predict_log_proba(self, X):
def predict_log_proba(self, X, **predict_log_proba_params):
"""Apply transforms, and predict_log_proba of the final estimator

Parameters
Expand All @@ -522,14 +526,20 @@ def predict_log_proba(self, X):
Data to predict on. Must fulfill input requirements of first step
of the pipeline.

**predict_log_proba_params : dict of string -> object
Parameters to the ``predict_log_proba`` called at the end of all
transformations in the pipeline.

Returns
-------
y_score : array-like of shape (n_samples, n_classes)
"""
Xt = X
for _, name, transform in self._iter(with_final=False):
Xt = transform.transform(Xt)
return self.steps[-1][-1].predict_log_proba(Xt)
return self.steps[-1][-1].predict_log_proba(
Xt, **predict_log_proba_params
)

@property
def transform(self):
Expand Down
20 changes: 16 additions & 4 deletions 20 sklearn/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ def predict(self, X, got_attribute=False):
self.got_attribute = got_attribute
return self

def predict_proba(self, X, got_attribute=False):
self.got_attribute = got_attribute
return self

def predict_log_proba(self, X, got_attribute=False):
self.got_attribute = got_attribute
return self


def test_pipeline_init():
# Test the various init parameters of the pipeline.
Expand Down Expand Up @@ -449,12 +457,16 @@ def test_fit_predict_with_intermediate_fit_params():
assert 'should_succeed' not in pipe.named_steps['transf'].fit_params


def test_predict_with_predict_params():
# tests that Pipeline passes predict_params to the final estimator
# when predict is invoked
@pytest.mark.parametrize("method_name", [
"predict", "predict_proba", "predict_log_proba"
])
def test_predict_methods_with_predict_params(method_name):
# tests that Pipeline passes predict_* to the final estimator
# when predict_* is invoked
pipe = Pipeline([('transf', Transf()), ('clf', DummyEstimatorParams())])
pipe.fit(None, None)
pipe.predict(X=None, got_attribute=True)
method = getattr(pipe, method_name)
method(X=None, got_attribute=True)

assert pipe.named_steps['clf'].got_attribute

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.