imblearn.pipeline.Pipeline¶
-
class
imblearn.pipeline.
Pipeline
(steps, memory=None)[source][source]¶ Pipeline of transforms and resamples with a final estimator.
Sequentially apply a list of transforms, samples 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 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.
Parameters: steps : list
List of (name, transform) tuples (implementing fit/transform/fit_sample) 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
orsteps
to inspect estimators within the pipeline. Caching the transformers is advantageous when fitting is time consuming.Examples
>>> from collections import Counter >>> from sklearn.datasets import make_classification >>> from sklearn.cross_validation import train_test_split as tts >>> from sklearn.decomposition import PCA >>> from sklearn.neighbors import KNeighborsClassifier as KNN >>> from sklearn.metrics import classification_report >>> from imblearn.over_sampling import SMOTE >>> from imblearn.pipeline import Pipeline >>> X, y = make_classification(n_classes=2, class_sep=2, ... weights=[0.1, 0.9], n_informative=3, n_redundant=1, flip_y=0, ... n_features=20, n_clusters_per_class=1, n_samples=1000, random_state=10) >>> print('Original dataset shape {}'.format(Counter(y))) Original dataset shape Counter({1: 900, 0: 100}) >>> pca = PCA() >>> smt = SMOTE(random_state=42) >>> knn = KNN() >>> pipeline = Pipeline([('smt', smt), ('pca', pca), ('knn', knn)]) >>> X_train, X_test, y_train, y_test = tts(X, y, random_state=42) >>> pipeline.fit(X_train, y_train) Pipeline(...) >>> y_hat = pipeline.predict(X_test) >>> print(classification_report(y_test, y_hat)) precision recall f1-score support 0 0.87 1.00 0.93 26 1 1.00 0.98 0.99 224 avg / total 0.99 0.98 0.98 250
Attributes
named_steps (dict) Read-only attribute to access any step parameter by user given name. Keys are step names and values are steps parameters. Methods
decision_function
(*args, **kwargs)Apply transformers/samplers, and decision_function of the final fit
(X[, y])Fit the model fit_predict
(*args, **kwargs)Applies fit_predict of last step in pipeline after transforms. fit_sample
(*args, **kwargs)Fit the model and sample with the final estimator fit_transform
(X[, y])Fit the model and transform with the final estimator get_params
([deep])Get parameters for this estimator. predict
(*args, **kwargs)Apply transformers/samplers to the data, and predict with the final predict_log_proba
(*args, **kwargs)Apply transformers/samplers, and predict_log_proba of the final predict_proba
(*args, **kwargs)Apply transformers/samplers, and predict_proba of the final sample
(*args, **kwargs)Sample the data with the final estimator score
(*args, **kwargs)Apply transformers/samplers, and score with the final estimator set_params
(**kwargs)Set the parameters of this estimator. Methods
__init__
(steps[, memory])decision_function
(*args, **kwargs)Apply transformers/samplers, and decision_function of the final fit
(X[, y])Fit the model fit_predict
(*args, **kwargs)Applies fit_predict of last step in pipeline after transforms. fit_sample
(*args, **kwargs)Fit the model and sample with the final estimator fit_transform
(X[, y])Fit the model and transform with the final estimator get_params
([deep])Get parameters for this estimator. predict
(*args, **kwargs)Apply transformers/samplers to the data, and predict with the final predict_log_proba
(*args, **kwargs)Apply transformers/samplers, and predict_log_proba of the final predict_proba
(*args, **kwargs)Apply transformers/samplers, and predict_proba of the final sample
(*args, **kwargs)Sample the data with the final estimator score
(*args, **kwargs)Apply transformers/samplers, and score with the final estimator set_params
(**kwargs)Set the parameters of this estimator. Attributes
classes_
inverse_transform
Apply inverse transformations in reverse order named_steps
transform
Apply transformers/samplers, and transform with the final estimator -
decision_function
(*args, **kwargs)[source][source]¶ Apply transformers/samplers, 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 : array-like, shape = [n_samples, n_classes]
-
fit
(X, y=None, **fit_params)[source][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, 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 parameterp
for steps
has keys__p
.Returns: self : Pipeline
This estimator
-
fit_predict
(*args, **kwargs)[source][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, 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 parameterp
for steps
has keys__p
.Returns: y_pred : array-like
-
fit_sample
(*args, **kwargs)[source][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_sample 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 parameterp
for steps
has keys__p
.Returns: Xt : array-like, shape = [n_samples, n_transformed_features]
Transformed samples
yt : array-like, shape = [n_samples, n_transformed_features]
Transformed target
-
fit_transform
(X, y=None, **fit_params)[source][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 parameterp
for steps
has keys__p
.Returns: Xt : array-like, shape = [n_samples, n_transformed_features]
Transformed samples
-
get_params
(deep=True)[source][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 : mapping of string to any
Parameter names mapped to their values.
-
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 andn_features
is the number of features. Must fulfill input requirements of last step of pipeline’sinverse_transform
method.Returns: Xt : array-like, shape = [n_samples, n_features]
-
predict
(*args, **kwargs)[source][source]¶ Apply transformers/samplers 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.
Returns: y_pred : array-like
-
predict_log_proba
(*args, **kwargs)[source][source]¶ Apply transformers/samplers, 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 : array-like, shape = [n_samples, n_classes]
-
predict_proba
(*args, **kwargs)[source][source]¶ Apply transformers/samplers, 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 : array-like, shape = [n_samples, n_classes]
-
sample
(*args, **kwargs)[source][source]¶ Sample the data with the final estimator
Applies transformers/samplers to the data, and the sample method of the final estimator. Valid only if the final estimator implements sample.
Parameters: X : iterable
Data to predict on. Must fulfill input requirements of first step of the pipeline.
-
score
(*args, **kwargs)[source][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, default=None
Targets used for scoring. Must fulfill label requirements for all steps of the pipeline.
sample_weight : array-like, default=None
If not None, this argument is passed as
sample_weight
keyword argument to thescore
method of the final estimator.Returns: score : float
-
set_params
(**kwargs)[source][source]¶ Set the parameters of this estimator.
Valid parameter keys can be listed with
get_params()
.Returns: self
-
transform
¶ Apply transformers/samplers, 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, n_transformed_features]
-