8000 [MRG] MNT remove coupling between pipeline methods by NicolasHug · Pull Request #16777 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

[MRG] MNT remove coupling between pipeline methods #16777

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 1 commit into from
Mar 27, 2020
Merged
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
54 changes: 33 additions & 21 deletions sklearn/pipeline.py
8000 86D1
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,7 @@ def _log_message(self, step_idx):
len(self.steps),
8000 name)

# Estimator interface

def _fit(self, X, y=None, **fit_params):
# shallow copy of steps - this should really be steps_
self.steps = list(self.steps)
self._validate_steps()
# Setup the memory
memory = check_memory(self.memory)

fit_transform_one_cached = memory.cache(_fit_transform_one)

def _check_fit_params(self, **fit_params):
fit_params_steps = {name: {} for name, step in self.steps
if step is not None}
for pname, pval in fit_params.items():
Expand All @@ -281,6 +271,19 @@ def _fit(self, X, y=None, **fit_params):
"=sample_weight)`.".format(pname))
step, param = pname.split('__', 1)
fit_params_steps[step][param] = pval
return fit_params_steps

# Estimator interface

def _fit(self, X, y=None, **fit_params_steps):
# shallow copy of steps - this should really be steps_
self.steps = list(self.steps)
self._validate_steps()
# Setup the memory
memory = check_memory(self.memory)

fit_transform_one_cached = memory.cache(_fit_transform_one)

for (step_idx,
name,
transformer) in self._iter(with_final=False,
Expand Down Expand Up @@ -318,9 +321,7 @@ def _fit(self, X, y=None, **fit_params):
# transformer. This is necessary when loading the transformer
# from the cache.
self.steps[step_idx] = (name, fitted_transformer)
if self._final_estimator == 'passthrough':
return X, {}
return X, fit_params_steps[self.steps[-1][0]]
return X

def fit(self, X, y=None, **fit_params):
"""Fit the model
Expand Down Expand Up @@ -348,11 +349,14 @@ def fit(self, X, y=None, **fit_params):
self : Pipeline
This estimator
"""
Xt, fit_params = self._fit(X, y, **fit_params)
fit_params_steps = self._check_fit_params(**fit_params)
Xt = self._fit(X, y, **fit_params_steps)
with _print_elapsed_time('Pipeline',
self._log_message(len(self.steps) - 1)):
if self._final_estimator != 'passthrough':
self._final_estimator.fit(Xt, y, **fit_params)
fit_params_last_step = fit_params_steps[self.steps[-1][0]]
self._final_estimator.fit(Xt, y, **fit_params_last_step)

return self

def fit_transform(self, X, y=None, **fit_params):
Expand Down Expand Up @@ -382,16 +386,20 @@ def fit_transform(self, X, y=None, **fit_params):
Xt : array-like of shape (n_samples, n_transformed_features)
Transformed samples
"""
fit_params_steps = self._check_fit_params(**fit_params)
Xt = self._fit(X, y, **fit_params_steps)

last_step = self._final_estimator
Xt, fit_params = self._fit(X, y, **fit_params)
with _print_elapsed_time('Pipeline',
self._log_message(len(self.steps) - 1)):
if last_step == 'passthrough':
return Xt
fit_params_last_step = fit_params_steps[self.steps[-1][0]]
if hasattr(last_step, 'fit_transform'):
return last_step.fit_transform(Xt, y, **fit_params)
return last_step.fit_transform(Xt, y, **fit_params_last_step)
else:
return last_step.fit(Xt, y, **fit_params).transform(Xt)
return last_step.fit(Xt, y,
**fit_params_last_step).transform(Xt)

@if_delegate_has_method(delegate='_final_estimator')
def predict(self, X, **predict_params):
Expand Down Expand Up @@ -447,10 +455,14 @@ def fit_predict(self, X, y=None, **fit_params):
-------
y_pred : array-like
"""
Xt, fit_params = self._fit(X, y, **fit_params)
fit_params_steps = self._check_fit_params(**fit_params)
Xt = self._fit(X, y, **fit_params_steps)

fit_params_last_step = fit_params_steps[self.steps[-1][0]]
with _print_elapsed_time('Pipeline',
self._log_message(len(self.steps) - 1)):
y_pred = self.steps[-1][-1].fit_predict(Xt, y, **fit_params)
y_pred = self.steps[-1][-1].fit_predict(Xt, y,
**fit_params_last_step)
return y_pred

@if_delegate_has_method(delegate='_final_estimator')
Expand Down
0