Pipeline

class gtda.pipeline.Pipeline(steps, memory=None, verbose=False)[source]

Pipeline of transforms and resamples with a final estimator.

Sequentially apply a list of transforms, sampling, and a final estimator. Intermediate steps of the pipeline must be transformers or resamplers, that is, they must implement fit, transform and sample methods. The samplers are only applied during fit. The final estimator only needs to implement fit. The transformers and samplers in the pipeline can be cached using memory argument.

The purpose of the pipeline is to assemble several steps that can be cross-validated together while setting different parameters. For this, it enables setting parameters of the various steps using their names and the parameter name separated by a ‘__’, as in the example below. A step’s estimator may be replaced entirely by setting the parameter with its name to another estimator, or a transformer removed by setting it to ‘passthrough’ or None.

Parameters
  • steps (list) – List of (name, transform) tuples (implementing fit/transform) that are chained, in the order in which they are chained, with the last object an estimator.

  • memory (Instance of joblib.Memory or string, optional (default: None)) – 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.

named_steps

Read-only attribute to access any step parameter by user given name. Keys are step names and values are steps parameters.

Type

dict

See also

make_pipeline

helper function to make pipeline.

Examples

>>> import numpy as np
>>> import gtda.time_series as ts
>>> import gtda.homology as hl
>>> import gtda.diagrams as diag
>>> from gtda.pipeline import Pipeline
>>> import sklearn.preprocessing as skprep
>>>
>>> X = np.random.rand(600, 1)
>>> n_train, n_test = 400, 200
>>>
>>> labeller = ts.Labeller(width=5, percentiles=[80],
>>>                        n_steps_future=1)
>>> X_train = X[:n_train]
>>> y_train = X_train
>>> X_train, y_train = labeller.fit_transform_resample(X_train, y_train)
>>>
>>> print(X_train.shape, y_train.shape)
(395, 1) (395,)
>>> steps = [
>>>     ('embedding', ts.TakensEmbedding()),
>>>     ('window', ts.SlidingWindow(width=5, stride=1)),
>>>     ('diagram', hl.VietorisRipsPersistence()),
>>>     ('rescaler', diag.Scaler()),
>>>     ('filter', diag.Filtering(epsilon=0.1)),
>>>     ('entropy', diag.PersistenceEntropy()),
>>>     ('scaling', skprep.MinMaxScaler(copy=True)),
>>> ]
>>> pipeline = Pipeline(steps)
>>>
>>> Xt_train, yr_train = pipeline.\
>>>     fit_transform_resample(X_train, y_train)
>>>
>>> print(X_train_final.shape, y_train_final.shape)
(389, 2) (389,)
__init__(steps, memory=None, verbose=False)[source]

Initialize self. See help(type(self)) for accurate signature.

decision_function(X)[source]

Apply transforms, and decision_function of the final estimator

Parameters

X (iterable) – Data to predict on. Must fulfill input requirements of first step of the pipeline.

Returns

y_score

Return type

array-like of shape (n_samples, n_classes)

fit(X, y=None, **fit_params)[source]

Fit the model.

Fit all the transforms/samplers one after the other and transform/sample the data, then fit the transformed/sampled data using the final estimator.

Parameters
  • X (iterable) – Training data. Must fulfill input requirements of first step of the pipeline.

  • y (iterable or None, default: None) – Training targets. Must fulfill label requirements for all steps of the pipeline.

  • **fit_params (dict of string -> object) – Parameters passed to the fit method of each step, where each parameter name is prefixed such that parameter p for step s has key s__p.

Returns

self – This estimator

Return type

Pipeline

fit_predict(X, y=None, **fit_params)[source]

Applies fit_predict of last step in pipeline after transforms.

Applies fit_transforms of a pipeline to the data, followed by the fit_predict method of the final estimator in the pipeline. Valid only if the final estimator implements fit_predict.

Parameters
  • X (iterable) – Training data. Must fulfill input requirements of first step of the pipeline.

  • y (iterable or None, default: None) – Training targets. Must fulfill label requirements for all steps of the pipeline.

  • **fit_params (dict of string -> object) – Parameters passed to the fit method of each step, where each parameter name is prefixed such that parameter p for step s has key s__p.

Returns

y_pred

Return type

array-like

fit_transform(X, y=None, **fit_params)[source]

Fit the model and transform with the final estimator.

Fits all the transformers/samplers one after the other and transform/sample the data, then uses fit_transform on transformed data with the final estimator.

Parameters
  • X (iterable) – Training data. Must fulfill input requirements of first step of the pipeline.

  • y (iterable, default: None) – Training targets. Must fulfill label requirements for all steps of the pipeline.

  • **fit_params (dict of string -> object) – Parameters passed to the fit method of each step, where each parameter name is prefixed such that parameter p for step s has key s__p.

Returns

Xt – Transformed samples

Return type

array-like, shape (n_samples, n_transformed_features)

fit_transform_resample(X, y=None, **fit_params)[source]

Fit the model and sample with the final estimator.

Fits all the transformers/samplers one after the other and transform/sample the data, then uses fit_resample on transformed data with the final estimator.

Parameters
  • X (iterable) – Training data. Must fulfill input requirements of first step of the pipeline.

  • y (iterable, default: None) – Training targets. Must fulfill label requirements for all steps of the pipeline.

  • **fit_params (dict of string -> object) – Parameters passed to the fit method of each step, where each parameter name is prefixed such that parameter p for step s has key s__p.

Returns

  • Xt (array-like, shape (n_samples, n_transformed_features)) – Transformed samples.

  • yr (array-like, shape (n_samples, n_transformed_features)) – Transformed target.

get_params(deep=True)[source]

Get parameters for this estimator.

Parameters

deep (boolean, optional) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns

params – Parameter names mapped to their values.

Return type

mapping of string to any

property inverse_transform

Apply inverse transformations in reverse order

All estimators in the pipeline must support inverse_transform.

Parameters

Xt (array-like, shape (n_samples, n_transformed_features)) – Data samples, where n_samples is the number of samples and n_features is the number of features. Must fulfill input requirements of last step of pipeline’s inverse_transform method.

Returns

Xt

Return type

array-like, shape (n_samples, n_features)

predict(X, **predict_params)[source]

Apply transforms to the data, and predict with the final estimator

Parameters
  • X (iterable) – Data to predict on. Must fulfill input requirements of first step of the pipeline.

  • **predict_params (dict of string -> object) – Parameters to the predict called at the end of all transformations in the pipeline. Note that while this may be used to return uncertainties from some models with return_std or return_cov, uncertainties that are generated by the transformations in the pipeline are not propagated to the final estimator.

Returns

y_pred

Return type

array-like

predict_log_proba(X)[source]

Apply transforms, and predict_log_proba of the final estimator

Parameters

X (iterable) – Data to predict on. Must fulfill input requirements of first step of the pipeline.

Returns

y_score

Return type

array-like of shape (n_samples, n_classes)

predict_proba(X)[source]

Apply transforms, and predict_proba of the final estimator

Parameters

X (iterable) – Data to predict on. Must fulfill input requirements of first step of the pipeline.

Returns

y_proba

Return type

array-like of shape (n_samples, n_classes)

property resample

Apply transformers/transformer_resamplers, and transform with the final estimator.

This also works where final estimator is None: all prior transformations are applied.

Parameters

y (array-like, shape = (n_samples,)) – Data to resample. Must fulfill input requirements of first step of the pipeline.

Returns

yr

Return type

array-like, shape = (n_samples_new,)

score(X, y=None, sample_weight=None)[source]

Apply transformers/samplers, and score with the final estimator

Parameters
  • X (iterable) – Data to predict on. Must fulfill input requirements of first step of the pipeline.

  • y (iterable or None, default: None) – Targets used for scoring. Must fulfill label requirements for all steps of the pipeline.

  • sample_weight (array-like or None, default: None) – If not None, this argument is passed as sample_weight keyword argument to the score method of the final estimator.

Returns

score

Return type

float

score_samples(X)[source]

Apply transforms, and score_samples of the final estimator.

Parameters

X (iterable) – Data to predict on. Must fulfill input requirements of first step of the pipeline.

Returns

y_score

Return type

ndarray, shape (n_samples,)

set_params(**kwargs)[source]

Set the parameters of this estimator.

Valid parameter keys can be listed with get_params().

Returns

Return type

self

property transform

Apply transformers/transformer_resamplers, and transform with the final estimator.

This also works where final estimator is None: all prior transformations are applied.

Parameters

X (iterable) – Data to transform. Must fulfill input requirements of first step of the pipeline.

Returns

Xt

Return type

array-like, shape (n_samples, n_transformed_features)

property transform_resample

Apply transformers/transformer_resamplers, and transform with the final estimator.

This also works where final estimator is None: all prior transformations are applied.

Parameters

X (iterable) – Data to transform. Must fulfill input requirements of first step of the pipeline.

Returns

  • Xt (array-like, shape = (n_samples_new, n_transformed_features))

  • yr (array-like, shape = (n_samples_new,))