8000 [WIP] Add verbose option for Pipeline and Feature Union [rebased 8568] by petrushev · Pull Request #9668 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

[WIP] Add verbose option for Pipeline and Feature Union [rebased 8568] #9668

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

Closed
Closed
17 changes: 9 additions & 8 deletions doc/modules/pipeline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ is an estimator object::
>>> pipe # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
Pipeline(memory=None,
steps=[('reduce_dim', PCA(copy=True,...)),
('clf', SVC(C=1.0,...))])
('clf', SVC(C=1.0,...))], verbose=False)

The utility function :func:`make_pipeline` is a shorthand
for constructing pipelines;
Expand All @@ -62,7 +62,7 @@ filling in the names automatically::
steps=[('binarizer', Binarizer(copy=True, threshold=0.0)),
('multinomialnb', MultinomialNB(alpha=1.0,
class_prior=None,
fit_prior=True))])
fit_prior=True))], verbose=False)

The estimators of a pipeline are stored as a list in the ``steps`` attribute::

Expand All @@ -82,7 +82,8 @@ Parameters of the estimators in the pipeline can be accessed using the
>>> pipe.set_params(clf__C=10) # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
Pipeline(memory=None,
steps=[('reduce_dim', PCA(copy=True, iterated_power='auto',...)),
('clf', SVC(C=10, cache_size=200, class_weight=None,...))])
('clf', SVC(C=10, cache_size=200, class_weight=None,...))],
verbose=False)

Attributes of named_steps map to keys, enabling tab completion in interactive environments::

Expand Down Expand Up @@ -160,7 +161,7 @@ object::
>>> pipe # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
Pipeline(...,
steps=[('reduce_dim', PCA(copy=True,...)),
('clf', SVC(C=1.0,...))])
('clf', SVC(C=1.0,...))], verbose=False)
>>> # Clear the cache directory when you don't need it anymore
>>> rmtree(cachedir)

Expand All @@ -177,7 +178,7 @@ object::
>>> pipe.fit(digits.data, digits.target)
... # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
Pipeline(memory=None,
steps=[('reduce_dim', PCA(...)), ('clf', SVC(...))])
steps=[('reduce_dim', PCA(...)), ('clf', SVC(...))], verbose=False)
>>> # The pca instance can be inspected directly
>>> print(pca1.components_) # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
[[ -1.77484909e-19 ... 4.07058917e-18]]
Expand All @@ -199,7 +200,7 @@ object::
>>> cached_pipe.fit(digits.data, digits.target)
... # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
Pipeline(memory=...,
steps=[('reduce_dim', PCA(...)), ('clf', SVC(...))])
steps=[('reduce_dim', PCA(...)), ('clf', SVC(...))], verbose=False)
>>> print(cached_pipe.named_steps['reduce_dim'].components_)
... # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
[[ -1.77484909e-19 ... 4.07058917e-18]]
Expand Down Expand Up @@ -253,7 +254,7 @@ and ``value`` is an estimator object::
FeatureUnion(n_jobs=1,
transformer_list=[('linear_pca', PCA(copy=True,...)),
('kernel_pca', KernelPCA(alpha=1.0,...))],
transformer_weights=None)
transformer_weights=None, verbose=False)


Like pipelines, feature unions have a shorthand constructor called
Expand All @@ -268,7 +269,7 @@ and ignored by setting to ``None``::
FeatureUnion(n_jobs=1,
transformer_list=[('linear_pca', PCA(copy=True,...)),
('kernel_pca', None)],
transformer_weights=None)
transformer_weights=None, verbose=False)

.. topic:: Examples:

Expand Down
13 changes: 13 additions & 0 deletions doc/whats_new.rst
8000
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ Model evaluation and meta-estimators
- A scorer based on :func:`metrics.brier_score_loss` is also available.
:issue:`9521` by :user:`Hanmin Qin <qinhanmin2014>`.

Miscellaneous

- Added optional parameter ``verbose`` in :class:`pipeline.Pipeline` and
:class:`pipeline.FeatureUnion` for showing progress and timing of each
step. :issue:`8568` by :user:`Karan Desai <karandesai-96>`.

- Added optional parameter ``verbose`` in functions `pipeline.make_pipeline`
and `pipeline.make_union` to extend the same functionality as the
corresponding classes. :issue:`9668` by
:user:`Baze Petrushev <petrushev>` and :user:`Karan Desai <karandesai-96>`.


Bug fixes
.........

Expand Down Expand Up @@ -5754,3 +5766,4 @@ David Huard, Dave Morrill, Ed Schofield, Travis Oliphant, Pearu Peterson.

.. _Neeraj Gangwar: http://neerajgangwar.in
.. _Arthur Mensch: https://amensch.fr
.. _Karan Desai: https://www.github.com/karandesai-96
6 changes: 3 additions & 3 deletions sklearn/cross_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import scipy.sparse as sp

from .base import is_classifier, clone
from .utils import indexable, check_random_state, safe_indexing
from .utils import (indexable, check_random_state, safe_indexing,
message_with_time)
from .utils.validation import (_is_arraylike, _num_samples,
column_or_1d)
from .utils.multiclass import type_of_target
Expand Down Expand Up @@ -1700,8 +1701,7 @@ def _fit_and_score(estimator, X, y, scorer, train, test, verbose,
if verbose > 2:
msg += ", score=%f" % test_score
if verbose > 1:
end_msg = "%s -%s" % (msg, logger.short_format_time(scoring_time))
print("[CV] %s %s" % ((64 - len(end_msg)) * '.', end_msg))
print(message_with_time('CV', msg, scoring_time))

ret = [train_score] if return_train_score else []
ret.extend([test_score, _num_samples(X_test), scoring_time])
Expand Down
8 changes: 4 additions & 4 deletions sklearn/model_selection/_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
import scipy.sparse as sp

from ..base import is_classifier, clone
from ..utils import indexable, check_random_state, safe_indexing
from ..utils import (indexable, check_random_state, safe_indexing,
message_with_time)
from ..utils.validation import _is_arraylike, _num_samples
from ..utils.metaestimators import _safe_split
from ..externals.joblib import Parallel, delayed, logger
from ..externals.joblib import Parallel, delayed
from ..externals.six.moves import zip
from ..metrics.scorer import check_scoring, _check_multimetric_scoring
from ..exceptions import FitFailedWa 6D40 rning
Expand Down Expand Up @@ -480,8 +481,7 @@ def _fit_and_score(estimator, X, y, scorer, train, test, verbose,
msg += ", score=%s" % test_scores
if verbose > 1:
total_time = score_time + fit_time
end_msg = "%s, total=%s" % (msg, logger.short_format_time(total_time))
print("[CV] %s %s" % ((64 - len(end_msg)) * '.', end_msg))
print(message_with_time('CV', msg, total_time))

ret = [train_scores, test_scores] if return_train_score else [test_scores]

Expand Down
Loading
0