80

I got this from the sklearn webpage:

  • Pipeline: Pipeline of transforms with a final estimator

  • Make_pipeline: Construct a Pipeline from the given estimators. This is a shorthand for the Pipeline constructor.

But I still do not understand when I have to use each one. Can anyone give me an example?

1 Answer 1

133

The only difference is that make_pipeline generates names for steps automatically.

Step names are needed e.g. if you want to use a pipeline with model selection utilities (e.g. GridSearchCV). With grid search you need to specify parameters for various steps of a pipeline:

pipe = Pipeline([('vec', CountVectorizer()), ('clf', LogisticRegression()])
param_grid = [{'clf__C': [1, 10, 100, 1000]}
gs = GridSearchCV(pipe, param_grid)
gs.fit(X, y)

compare it with make_pipeline:

pipe = make_pipeline(CountVectorizer(), LogisticRegression())     
param_grid = [{'logisticregression__C': [1, 10, 100, 1000]}
gs = GridSearchCV(pipe, param_grid)
gs.fit(X, y)

So, with Pipeline:

  • names are explicit, you don't have to figure them out if you need them;
  • name doesn't change if you change estimator/transformer used in a step, e.g. if you replace LogisticRegression() with LinearSVC() you can still use clf__C.

make_pipeline:

  • shorter and arguably more readable notation;
  • names are auto-generated using a straightforward rule (lowercase name of an estimator).

When to use them is up to you :) I prefer make_pipeline for quick experiments and Pipeline for more stable code; a rule of thumb: IPython Notebook -> make_pipeline; Python module in a larger project -> Pipeline. But it is certainly not a big deal to use make_pipeline in a module or Pipeline in a short script or a notebook.

6
  • Could you tell me where it is documented that the name of LogisticRegression()'s estimator is logisticregression? I had to set a grid search for OneVsRestClassifier(LinearSVC()) but I don't know what name refers to it.
    – KubiK888
    Sep 14, 2019 at 18:00
  • @KubiK888 it is documented at scikit-learn.org/stable/modules/generated/… - "their names will be set to the lowercase of their types automatically" Sep 16, 2019 at 13:38
  • But what about OneVsRestClassifier(LinearSVC()), I have tried all of the following: 'onevsrestclassifier_linearsvc__C', onevsrestclassifier_linearsvc_estimator__C', 'onevsrestclassifier__C', 'linearsvc__C', 'onevsrestclassifier__linearsvc__C', 'onevsrestclassifier-linearsvc__C', 'onevsrestclassifier_linearsvc_estimator__C', 'estimator__C', they all give me Check the list of available parameters with "estimator.get_params().keys()".
    – KubiK888
    Sep 16, 2019 at 15:34
  • 2
    Never mine, just found out this is the correct naming format onevsrestclassifier__estimator__C.
    – KubiK888
    Sep 16, 2019 at 18:30
  • @KubiK888 Where did you find the correct naming format? Is there a list somewhere? Oct 13, 2019 at 4:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.