Python - What is exactly sklearn.pipeline.Pipeline?

后端 未结 3 711
灰色年华
灰色年华 2020-11-30 16:07

I can\'t figure out how the sklearn.pipeline.Pipeline works exactly.

There are a few explanation in the doc. For example what do they mean by:

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 17:07

    Transformer in scikit-learn - some class that have fit and transform method, or fit_transform method.

    Predictor - some class that has fit and predict methods, or fit_predict method.

    Pipeline is just an abstract notion, it's not some existing ml algorithm. Often in ML tasks you need to perform sequence of different transformations (find set of features, generate new features, select only some good features) of raw dataset before applying final estimator.

    Here is a good example of Pipeline usage. Pipeline gives you a single interface for all 3 steps of transformation and resulting estimator. It encapsulates transformers and predictors inside, and now you can do something like:

        vect = CountVectorizer()
        tfidf = TfidfTransformer()
        clf = SGDClassifier()
    
        vX = vect.fit_transform(Xtrain)
        tfidfX = tfidf.fit_transform(vX)
        predicted = clf.fit_predict(tfidfX)
    
        # Now evaluate all steps on test set
        vX = vect.fit_transform(Xtest)
        tfidfX = tfidf.fit_transform(vX)
        predicted = clf.fit_predict(tfidfX)
    

    With just:

    pipeline = Pipeline([
        ('vect', CountVectorizer()),
        ('tfidf', TfidfTransformer()),
        ('clf', SGDClassifier()),
    ])
    predicted = pipeline.fit(Xtrain).predict(Xtrain)
    # Now evaluate all steps on test set
    predicted = pipeline.predict(Xtest)
    

    With pipelines you can easily perform a grid-search over set of parameters for each step of this meta-estimator. As described in the link above. All steps except last one must be transforms, last step can be transformer or predictor. Answer to edit: When you call pipln.fit() - each transformer inside pipeline will be fitted on outputs of previous transformer (First transformer is learned on raw dataset). Last estimator may be transformer or predictor, you can call fit_transform() on pipeline only if your last estimator is transformer (that implements fit_transform, or transform and fit methods separately), you can call fit_predict() or predict() on pipeline only if your last estimator is predictor. So you just can't call fit_transform or transform on pipeline, last step of which is predictor.

提交回复
热议问题