Skip to content

Add memory to make_pipeline function #458

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
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: 6 additions & 2 deletions doc/whats_new/v0.0.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ Bug fixes

- Fix bug which was not preserving the dtype of X and y when generating
samples.
issue:`448` by :user:`Guillaume Lemaitre <glemaitre>`.
:issue:`448` by :user:`Guillaume Lemaitre <glemaitre>`.

- Add the option to pass a ``Memory`` object to :func:`make_pipeline` like
in :class:`pipeline.Pipeline` class.
:issue:`458` by :user:`Christos Aridas <chkoar>`.

Maintenance
...........
Expand Down Expand Up @@ -117,4 +121,4 @@ Deprecation
- Deprecate :class:`imblearn.ensemble.EasyEnsemble` in favor of meta-estimator
:class:`imblearn.ensemble.EasyEnsembleClassifier` which follow the exact
algorithm described in the literature.
:issue:`455` by :user:`Guillaume Lemaitre <glemaitre>`.
:issue:`455` by :user:`Guillaume Lemaitre <glemaitre>`.
39 changes: 37 additions & 2 deletions imblearn/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,15 +600,50 @@ def _fit_sample_one(sampler, X, y, **fit_params):
return X_res, y_res, sampler


def make_pipeline(*steps):
def make_pipeline(*steps, **kwargs):
"""Construct a Pipeline from the given estimators.

This is a shorthand for the Pipeline constructor; it does not require, and
does not permit, naming the estimators. Instead, their names will be set
to the lowercase of their types automatically.

Parameters
----------
*steps : list of estimators.

memory : None, str or object with the joblib.Memory interface, optional
Used to cache the fitted transformers of the pipeline. By default,
no caching is performed. If a string is given, it is the path to
the caching directory. Enabling caching triggers a clone of
the transformers before fitting. Therefore, the transformer
instance given to the pipeline cannot be inspected
directly. Use the attribute ``named_steps`` or ``steps`` to
inspect estimators within the pipeline. Caching the
transformers is advantageous when fitting is time consuming.

Returns
-------
p : Pipeline

See also
--------
imblearn.pipeline.Pipeline : Class for creating a pipeline of
transforms with a final estimator.

Examples
--------
>>> from sklearn.naive_bayes import GaussianNB
>>> from sklearn.preprocessing import StandardScaler
>>> make_pipeline(StandardScaler(), GaussianNB(priors=None))
... # doctest: +NORMALIZE_WHITESPACE
Pipeline(memory=None,
steps=[('standardscaler',
StandardScaler(copy=True, with_mean=True, with_std=True)),
('gaussiannb',
GaussianNB(priors=None))])
"""
return Pipeline(pipeline._name_estimators(steps))
memory = kwargs.pop('memory', None)
if kwargs:
raise TypeError('Unknown keyword arguments: "{}"'
.format(list(kwargs.keys())[0]))
return Pipeline(pipeline._name_estimators(steps), memory=memory)
14 changes: 14 additions & 0 deletions imblearn/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import numpy as np
from pytest import raises

from sklearn.utils.testing import assert_true
from sklearn.utils.testing import assert_array_equal
from sklearn.utils.testing import assert_array_almost_equal
from sklearn.utils.testing import assert_allclose
Expand All @@ -31,6 +32,7 @@
from imblearn.under_sampling import (RandomUnderSampler,
EditedNearestNeighbours as ENN)


JUNK_FOOD_DOCS = (
"the pizza pizza beer copyright",
"the pizza burger beer copyright",
Expand Down Expand Up @@ -1073,3 +1075,15 @@ def test_pipeline_fit_then_sample_3_samplers_with_sampler_last_estimator():
X_fit_then_sample_res, y_fit_then_sample_res = pipeline.sample(X, y)
assert_array_equal(X_fit_sample_resampled, X_fit_then_sample_res)
assert_array_equal(y_fit_sample_resampled, y_fit_then_sample_res)


def test_make_pipeline_memory():
cachedir = mkdtemp()
try:
memory = Memory(cachedir=cachedir, verbose=10)
pipeline = make_pipeline(DummyTransf(), SVC(), memory=memory)
assert pipeline.memory is memory
pipeline = make_pipeline(DummyTransf(), SVC())
assert pipeline.memory is None
finally:
shutil.rmtree(cachedir)