giotto.pipeline
.Pipeline¶

class
giotto.pipeline.
Pipeline
(steps, memory=None, verbose=False)¶ 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 crossvalidated 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
 stepslist
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.
 memoryInstance 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
orsteps
to inspect estimators within the pipeline. Caching the transformers is advantageous when fitting is time consuming.
 Attributes
 named_stepsdict
Readonly attribute to access any step parameter by user given name. Keys are step names and values are steps parameters.
See also
make_pipeline
helper function to make pipeline.
Examples
>>> import numpy as np >>> import giotto.time_series as ts >>> import giotto.homology as hl >>> import giotto.diagrams as diag >>> from giotto.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,)
Methods
decision_function
(self, X)Apply transforms, and decision_function of the final estimator
fit
(self, X[, y])Fit the model.
fit_predict
(self, X[, y])Applies fit_predict of last step in pipeline after transforms.
fit_transform
(self, X[, y])Fit the model and transform with the final estimator.
fit_transform_resample
(self, X[, y])Fit the model and sample with the final estimator.
get_params
(self[, deep])Get parameters for this estimator.
predict
(self, X, \*\*predict_params)Apply transforms to the data, and predict with the final estimator
predict_log_proba
(self, X)Apply transforms, and predict_log_proba of the final estimator
predict_proba
(self, X)Apply transforms, and predict_proba of the final estimator
score
(self, X[, y, sample_weight])Apply transformers/samplers, and score with the final estimator
set_params
(self, \*\*kwargs)Set the parameters of this estimator.

__init__
(self, steps, memory=None, verbose=False)¶ Initialize self. See help(type(self)) for accurate signature.

decision_function
(self, X)¶ Apply transforms, and decision_function of the final estimator
 Parameters
 Xiterable
Data to predict on. Must fulfill input requirements of first step of the pipeline.
 Returns
 y_scorearraylike, shape = [n_samples, n_classes]

fit
(self, X, y=None, **fit_params)¶ 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
 Xiterable
Training data. Must fulfill input requirements of first step of the pipeline.
 yiterable or None, default:
None
Training targets. Must fulfill label requirements for all steps of the pipeline.
 **fit_paramsdict of string > object
Parameters passed to the
fit
method of each step, where each parameter name is prefixed such that parameterp
for steps
has keys__p
.
 Returns
 selfPipeline
This estimator

fit_predict
(self, X, y=None, **fit_params)¶ 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
 Xiterable
Training data. Must fulfill input requirements of first step of the pipeline.
 yiterable or None, default:
None
Training targets. Must fulfill label requirements for all steps of the pipeline.
 **fit_paramsdict of string > object
Parameters passed to the
fit
method of each step, where each parameter name is prefixed such that parameterp
for steps
has keys__p
.
 Returns
 y_predarraylike

fit_transform
(self, X, y=None, **fit_params)¶ 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
 Xiterable
Training data. Must fulfill input requirements of first step of the pipeline.
 yiterable, default:
None
Training targets. Must fulfill label requirements for all steps of the pipeline.
 **fit_paramsdict of string > object
Parameters passed to the
fit
method of each step, where each parameter name is prefixed such that parameterp
for steps
has keys__p
.
 Returns
 Xtarraylike, shape (n_samples, n_transformed_features)
Transformed samples

fit_transform_resample
(self, X, y=None, **fit_params)¶ 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
 Xiterable
Training data. Must fulfill input requirements of first step of the pipeline.
 yiterable, default:
None
Training targets. Must fulfill label requirements for all steps of the pipeline.
 **fit_paramsdict of string > object
Parameters passed to the
fit
method of each step, where each parameter name is prefixed such that parameterp
for steps
has keys__p
.
 Returns
 Xtarraylike, shape (n_samples, n_transformed_features)
Transformed samples.
 yrarraylike, shape (n_samples, n_transformed_features)
Transformed target.

get_params
(self, deep=True)¶ Get parameters for this estimator.
 Parameters
 deepboolean, optional
If True, will return the parameters for this estimator and contained subobjects that are estimators.
 Returns
 paramsmapping of string to any
Parameter names mapped to their values.

property
inverse_transform
¶ Apply inverse transformations in reverse order
All estimators in the pipeline must support
inverse_transform
. Parameters
 Xtarraylike, shape (n_samples, n_transformed_features)
Data samples, where
n_samples
is the number of samples andn_features
is the number of features. Must fulfill input requirements of last step of pipeline’sinverse_transform
method.
 Returns
 Xtarraylike, shape (n_samples, n_features)

predict
(self, X, **predict_params)¶ Apply transforms to the data, and predict with the final estimator
 Parameters
 Xiterable
Data to predict on. Must fulfill input requirements of first step of the pipeline.
 **predict_paramsdict 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_predarraylike

predict_log_proba
(self, X)¶ Apply transforms, and predict_log_proba of the final estimator
 Parameters
 Xiterable
Data to predict on. Must fulfill input requirements of first step of the pipeline.
 Returns
 y_scorearraylike, shape = [n_samples, n_classes]

predict_proba
(self, X)¶ Apply transforms, and predict_proba of the final estimator
 Parameters
 Xiterable
Data to predict on. Must fulfill input requirements of first step of the pipeline.
 Returns
 y_probaarraylike, 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
 yarraylike, shape = (n_samples, )
Data to resample. Must fulfill input requirements of first step of the pipeline.
 Returns
 yrarraylike, shape = (n_samples_new, )

score
(self, X, y=None, sample_weight=None)¶ Apply transformers/samplers, and score with the final estimator
 Parameters
 Xiterable
Data to predict on. Must fulfill input requirements of first step of the pipeline.
 yiterable or None, default:
None
Targets used for scoring. Must fulfill label requirements for all steps of the pipeline.
 sample_weightarraylike or None, default:
None
If not None, this argument is passed as
sample_weight
keyword argument to thescore
method of the final estimator.
 Returns
 scorefloat

set_params
(self, **kwargs)¶ Set the parameters of this estimator.
Valid parameter keys can be listed with
get_params()
. Returns
 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
 Xiterable
Data to transform. Must fulfill input requirements of first step of the pipeline.
 Returns
 Xtarraylike, 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
 Xiterable
Data to transform. Must fulfill input requirements of first step of the pipeline.
 Returns
 Xtarraylike, shape = (n_samples_new, n_transformed_features)
 yrarraylike, shape = (n_samples_new, )