sklearn.pipeline
.Pipeline¶
- class sklearn.pipeline.Pipeline(steps, *, memory=None, verbose=False)[source]¶
Pipeline of transforms with a final estimator.
Sequentially apply a list of transforms and a final estimator. Intermediate steps of the pipeline must be ‘transforms’, that is, they must implement
fit
andtransform
methods. The final estimator only needs to implementfit
. The transformers in the pipeline can be cached usingmemory
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'
orNone
.Read more in the User Guide.
New in version 0.5.
- Parameters
- stepslist of tuple
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.- memorystr or object with the joblib.Memory interface, 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.- verbosebool, default=False
If True, the time elapsed while fitting each step will be printed as it is completed.
- Attributes
named_steps
Bunch
Access the steps by name.
classes_
ndarray of shape (n_classes,)The classes labels.
n_features_in_
intNumber of features seen during first step
fit
method.feature_names_in_
ndarray of shape (n_features_in_
,)Names of features seen during first step
fit
method.
See also
make_pipeline
Convenience function for simplified pipeline construction.
Examples
>>> from sklearn.svm import SVC >>> from sklearn.preprocessing import StandardScaler >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn.pipeline import Pipeline >>> X, y = make_classification(random_state=0) >>> X_train, X_test, y_train, y_test = train_test_split(X, y, ... random_state=0) >>> pipe = Pipeline([('scaler', StandardScaler()), ('svc', SVC())]) >>> # The pipeline can be used as any other estimator >>> # and avoids leaking the test set into the train set >>> pipe.fit(X_train, y_train) Pipeline(steps=[('scaler', StandardScaler()), ('svc', SVC())]) >>> pipe.score(X_test, y_test) 0.88
Methods
Transform the data, and apply
decision_function
with the final estimator.fit
(X[, y])Fit the model.
fit_predict
(X[, y])Transform the data, and apply
fit_predict
with the final estimator.fit_transform
(X[, y])Fit the model and transform with the final estimator.
get_feature_names_out
([input_features])Get output feature names for transformation.
get_params
([deep])Get parameters for this estimator.
Apply
inverse_transform
for each step in a reverse order.predict
(X, **predict_params)Transform the data, and apply
predict
with the final estimator.predict_log_proba
(X, **predict_log_proba_params)Transform the data, and apply
predict_log_proba
with the final estimator.predict_proba
(X, **predict_proba_params)Transform the data, and apply
predict_proba
with the final estimator.score
(X[, y, sample_weight])Transform the data, and apply
score
with the final estimator.Transform the data, and apply
score_samples
with the final estimator.set_params
(**kwargs)Set the parameters of this estimator.
transform
(X)Transform the data, and apply
transform
with the final estimator.- property classes_¶
The classes labels. Only exist if the last step is a classifier.
- decision_function(X)[source]¶
Transform the data, and apply
decision_function
with the final estimator.Call
transform
of each transformer in the pipeline. The transformed data are finally passed to the final estimator that callsdecision_function
method. Only valid if the final estimator implementsdecision_function
.- Parameters
- Xiterable
Data to predict on. Must fulfill input requirements of first step of the pipeline.
- Returns
- y_scorendarray of shape (n_samples, n_classes)
Result of calling
decision_function
on the final estimator.
- property feature_names_in_¶
Names of features seen during first step
fit
method.
- fit(X, y=None, **fit_params)[source]¶
Fit the model.
Fit all the transformers one after the other and transform the data. Finally, fit the transformed data using 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
- selfobject
Pipeline with fitted steps.
- fit_predict(X, y=None, **fit_params)[source]¶
Transform the data, and apply
fit_predict
with the final estimator.Call
fit_transform
of each transformer in the pipeline. The transformed data are finally passed to the final estimator that callsfit_predict
method. Only valid if the final estimator implementsfit_predict
.- 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
- y_predndarray
Result of calling
fit_predict
on the final estimator.
- fit_transform(X, y=None, **fit_params)[source]¶
Fit the model and transform with the final estimator.
Fits all the transformers one after the other and transform 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
- Xtndarray of shape (n_samples, n_transformed_features)
Transformed samples.
- get_feature_names_out(input_features=None)[source]¶
Get output feature names for transformation.
Transform input features using the pipeline.
- Parameters
- input_featuresarray-like of str or None, default=None
Input features.
- Returns
- feature_names_outndarray of str objects
Transformed feature names.
- get_params(deep=True)[source]¶
Get parameters for this estimator.
Returns the parameters given in the constructor as well as the estimators contained within the
steps
of thePipeline
.- Parameters
- deepbool, default=True
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.
- inverse_transform(Xt)[source]¶
Apply
inverse_transform
for each step in a reverse order.All estimators in the pipeline must support
inverse_transform
.- Parameters
- Xtarray-like of 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
- Xtndarray of shape (n_samples, n_features)
Inverse transformed data, that is, data in the original feature space.
- property n_features_in_¶
Number of features seen during first step
fit
method.
- property named_steps¶
Access the steps by name.
Read-only attribute to access any step by given name. Keys are steps names and values are the steps objects.
- predict(X, **predict_params)[source]¶
Transform the data, and apply
predict
with the final estimator.Call
transform
of each transformer in the pipeline. The transformed data are finally passed to the final estimator that callspredict
method. Only valid if the final estimator implementspredict
.- 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.New in version 0.20.
- Returns
- y_predndarray
Result of calling
predict
on the final estimator.
- predict_log_proba(X, **predict_log_proba_params)[source]¶
Transform the data, and apply
predict_log_proba
with the final estimator.Call
transform
of each transformer in the pipeline. The transformed data are finally passed to the final estimator that callspredict_log_proba
method. Only valid if the final estimator implementspredict_log_proba
.- Parameters
- Xiterable
Data to predict on. Must fulfill input requirements of first step of the pipeline.
- **predict_log_proba_paramsdict of string -> object
Parameters to the
predict_log_proba
called at the end of all transformations in the pipeline.
- Returns
- y_log_probandarray of shape (n_samples, n_classes)
Result of calling
predict_log_proba
on the final estimator.
- predict_proba(X, **predict_proba_params)[source]¶
Transform the data, and apply
predict_proba
with the final estimator.Call
transform
of each transformer in the pipeline. The transformed data are finally passed to the final estimator that callspredict_proba
method. Only valid if the final estimator implementspredict_proba
.- Parameters
- Xiterable
Data to predict on. Must fulfill input requirements of first step of the pipeline.
- **predict_proba_paramsdict of string -> object
Parameters to the
predict_proba
called at the end of all transformations in the pipeline.
- Returns
- y_probandarray of shape (n_samples, n_classes)
Result of calling
predict_proba
on the final estimator.
- score(X, y=None, sample_weight=None)[source]¶
Transform the data, and apply
score
with the final estimator.Call
transform
of each transformer in the pipeline. The transformed data are finally passed to the final estimator that callsscore
method. Only valid if the final estimator implementsscore
.- Parameters
- Xiterable
Data to predict on. Must fulfill input requirements of first step of the pipeline.
- yiterable, default=None
Targets used for scoring. Must fulfill label requirements for all steps of the pipeline.
- sample_weightarray-like, default=None
If not None, this argument is passed as
sample_weight
keyword argument to thescore
method of the final estimator.
- Returns
- scorefloat
Result of calling
score
on the final estimator.
- score_samples(X)[source]¶
Transform the data, and apply
score_samples
with the final estimator.Call
transform
of each transformer in the pipeline. The transformed data are finally passed to the final estimator that callsscore_samples
method. Only valid if the final estimator implementsscore_samples
.- Parameters
- Xiterable
Data to predict on. Must fulfill input requirements of first step of the pipeline.
- Returns
- y_scorendarray of shape (n_samples,)
Result of calling
score_samples
on the final estimator.
- set_params(**kwargs)[source]¶
Set the parameters of this estimator.
Valid parameter keys can be listed with
get_params()
. Note that you can directly set the parameters of the estimators contained insteps
.- Parameters
- **kwargsdict
Parameters of this estimator or parameters of estimators contained in
steps
. Parameters of the steps may be set using its name and the parameter name separated by a ‘__’.
- Returns
- selfobject
Pipeline class instance.
- transform(X)[source]¶
Transform the data, and apply
transform
with the final estimator.Call
transform
of each transformer in the pipeline. The transformed data are finally passed to the final estimator that callstransform
method. Only valid if the final estimator implementstransform
.This also works where final estimator is
None
in which case all prior transformations are applied.- Parameters
- Xiterable
Data to transform. Must fulfill input requirements of first step of the pipeline.
- Returns
- Xtndarray of shape (n_samples, n_transformed_features)
Transformed data.