Randy Olson Randy Olson - 2 months ago 16
Python Question

Multiple pipelines that merge within a sklearn Pipeline?

Sometimes I design machine learning pipelines that look something like this:

Example pipeline

Normally I have to hack these "split" pipelines together using my own "Combine Features" function. However, it'd be great if I could fit this into a sklearn Pipeline object. How would I go about doing that? (Pseudo-code is fine.)

Answer

As long as "Entire Data Set" means the same features, this is exactly what FeatureUnion does:

make_pipeline(make_union(PolynomialFeatures(), PCA()), RFE(RandomForestClassifier()))

If you have two different sets of features that you want to combine, you first need to put them into a single dataset, and then have each branch of the FeatureUnion first select the features it should operate on. [there is currently no ready-made function for this but it's easily achiveable with a FunctionTransformer() for example]

Comments