You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Factory-style construction of composite estimators #16301
I find the API for specifying lists constituent estimators in Pipeline, FeatureUnion, ColumnTransformer, StackingClassifier, VotingClassifier, GeneralizedNB, etc. ugly. I find the make_pipeline, make_column_transformer etc interfaces helpful, but the duplication of interfaces is actually quite ugly too, and it is limiting: the moment you want to name one of your Pipeline steps explicitly, in order to make your grid search parameter space reasonably intelligible, you need to change from make_pipeline syntax to Pipeline syntax. Yuck!
I'd like to see a POC implementation and likely a SLEP towards creating a syntax like:
The append method would progressively construct the transformers parameter of est. Note that the first use of append does not give the body_text transfomer a name, so one will be automatically generated.
Note that since we delay parameter validation until fit, similar syntax is already possible:
So additional methods like append are just syntactic sugar for getting, amending and setting the steps/transformers parameter. But I think they would encourage named parameters instead of confusing tuples, creating more legible code.
Each of the above estimators would have methods similar to append or add (perhaps also insert).
PS: I have mentioned this proposal before, particularly when we ran into the inconsistent tuple ordering between make_column_transformer and ColumnTransformer.
Reacted by Venkatachalam N, Adrin Jalali and Helder Geovane Gomes de Lima
I find this interesting but I am concerned about supporting too many ways of achieving the same thing. I feel like this is what makes some libraries hard to use and learn.
I am concerned about supporting too many ways of achieving the same thing. I feel like this is what makes some libraries hard to use and learn.
I think it is a pity we did not consider this method-based syntax before we introduced make_pipeline etc.
The appeal of the method based approach is that it is much more explicit and self-checking: each add method has its own arguments and docstrings. The make_pipeline logic, OTOH, is completely implicit and the idea that an estimator is assigned a name automatically makes the whole thing a bit conceptually confusing.
The make_pipeline logic, OTOH, is completely implicit and the idea that an estimator is assigned a name automatically makes the whole thing a bit conceptually confusing.
I would be fairly happy with using a method for creating ColumnTransformers. For me, I like append because it treats ColumnTransformer like a list.
Side note: For 95% of the time, I use Pipeline and ColumnTransformer directly because I want to explicitly give names.
I suspect @amueller will have a few thoughts regarding constructing DAGs and/or pipelines.
But I think I like attach better in the context of the ColumnTransformer.
For Pipeline: append, add or chain would be more natural because the sequential structure is important.
I like the general idea and it's true that it's probably lead to more easy to read code, more inline with the conventions of the Deep Learning community for complex architectures and would also better interact with automated formatting tools such as black shall we use them in the future.
But I think I like attach better in the context of the ColumnTransformer.
For Pipeline: append, add or chain would be more natural because the sequential structure is important.
I think that we should use the same verb. I think that I like 'attach' too. It seems to work in many situations (Pipeline, ColumnTransformer, FeatureUnion, BaggingClassifier), and it's short.
Another suggestion bind (for column transformer at least).
I am not convinced we should use the same verb for ColumnTransformer and the Pipeline: only ColumnTransformer takes the column selector as (primary) argument for this builder method.
Having a different name for the method each time defeates the purpose of
object-oriented programming. I agree that the semantics vary slightly,
but we can still make the signatures very close.
I wanted to get a feel for how some of my teaching code would look under this proposal, so I created some examples. I started with Pipeline, since it's a bit easier to digest:
Option 1: This is what I use 95% of the time. It's easy to read and write, I know exactly what's in it (because of my own naming conventions), and I know exactly what the assigned step names will be.
Option 2: I don't like that I am required to name all of the steps, and I especially don't like writing or reading ([( and )]) at the start and end of every Pipeline. It's too easy to make mistakes.
Option 3 (no step names): I still prefer option 1, but option 3 is okay. I could get used to it.
Option 4 (step names): I like that the step names come after the objects and that the step names are optional. I don't particularly like wrapping the entire thing in parentheses. I'm not sure if I prefer option 2 or option 4.
Option 1: This is what I use 95% of the time. It's somewhat easy to make mistakes because of the number of commas and parentheses, but I've gotten used to it.
Option 2: I don't like that I am required to name all of the transformers. I find it hard to write because of the number of parentheses, commas, and brackets.
Option 3 (no transformer names): I'm okay with this. I may still have a slight preference for option 1.
Option 4 (transformer names): I like this a bit better than option 2, especially because the transformer names are optional.
Overall assessment
I think the case for this proposal is stronger for ColumnTransformer than for Pipeline.
I share @NicolasHug's concern of having three different ways to do the same thing. So much code is already out "in the wild" that uses the existing two options that even if we strongly recommended the new way, it's fair to assume that the existing two options will continue to be used for many years to come. That may make things especially tough for new users.
Assuming this proposal was accepted, it's not clear to me whether deprecating make_pipeline and make_column_transformer would make things better or worse.
Method naming
I like the idea of having one method name that is used with all composite estimators, since that is less for the user to remember.
At the moment, I like add the best because:
It feels the most natural: "Add this to your Pipeline" and "add this to your ColumnTransformer" both sound natural.
It's short: It's fast to type, and it doesn't overwhelm your eyes when reading it.
It doesn't imply an ordering: I wouldn't want to use a method name for ColumnTransformer that implies an ordering. Getting students to understand the difference between Pipeline and ColumnTransformer is already quite difficult, and I believe a method name like "append" would make that harder.
I'm not sure that your comparison is entirely fair, since if
pipe = make_pipeline(ct, logreg)
is sufficient, then so is:
pipe = Pipeline().add(ct).add(logreg)
In some ways this is poorer to read, but it is simpler in the sense that:
you always import Pipeline, and don't need to decide if Pipeline or make_pipeline is a better choice. (We could have facilitated that by making make_pipeline instead a classmethod Pipeline.make.)
it is easy to change one or both steps to having explicit names later.
It's been a year. I'd be very happy if we revive this. Tagging @scikit-learn/core-devs since it's also related to some of the efforts related to the sample props SLEP (SLEP006).
Seems like there's a [at least weak] consensus on a way forward.
I’d like the idea of an add method, but have the same concern as @NicolasHug#16301 (comment).
If we add something new, do we also want to get rid of something old?
I find this interesting but I am concerned about supporting too many ways of achieving the same thing. I feel like this is what makes some libraries hard to use and learn.
That's a good point... I think that if we add a "better" API, we should change the examples/tutorial to use it. Where a method is added to control a parameter, we should note in the parameter docstring that the method is preferred. This specific proposal might involve deprecating make_pipeline etc which are convenient, but add some obscurity.
Activity
rth commentedon Jul 25, 2020
Maybe we should mention this at the next dev meeting. I don't think many people saw this issue.
NicolasHug commentedon Jul 25, 2020
I find this interesting but I am concerned about supporting too many ways of achieving the same thing. I feel like this is what makes some libraries hard to use and learn.
Happy to consider a SLEP though.
jnothman commentedon Jul 26, 2020
I think it is a pity we did not consider this method-based syntax before we introduced
make_pipelineetc.The appeal of the method based approach is that it is much more explicit and self-checking: each
addmethod has its own arguments and docstrings. The make_pipeline logic, OTOH, is completely implicit and the idea that an estimator is assigned a name automatically makes the whole thing a bit conceptually confusing.jnothman commentedon Jul 26, 2020
thomasjpfan commentedon Jul 26, 2020
I would be fairly happy with using a method for creating
ColumnTransformers. For me, I likeappendbecause it treatsColumnTransformerlike a list.Side note: For 95% of the time, I use
PipelineandColumnTransformerdirectly because I want to explicitly give names.I suspect @amueller will have a few thoughts regarding constructing DAGs and/or pipelines.
jnothman commentedon Jul 26, 2020
[WIP] POC for Factory-style construction of composite estimators
GaelVaroquaux commentedon Jul 27, 2020
I think that the general idea must be explored, because we really have a need.
I would avoid the term "append"
thomasjpfan commentedon Jul 27, 2020
What would be a good alternative?
GaelVaroquaux commentedon Jul 27, 2020
The reasons why I would avoid "append" are:
Suggestions:
ogrisel commentedon Jul 27, 2020
Maybe also:
But I think I like
attachbetter in the context of theColumnTransformer.For
Pipeline:append,addorchainwould be more natural because the sequential structure is important.I like the general idea and it's true that it's probably lead to more easy to read code, more inline with the conventions of the Deep Learning community for complex architectures and would also better interact with automated formatting tools such as black shall we use them in the future.
GaelVaroquaux commentedon Jul 27, 2020
I think that we should use the same verb. I think that I like 'attach' too. It seems to work in many situations (Pipeline, ColumnTransformer, FeatureUnion, BaggingClassifier), and it's short.
ogrisel commentedon Jul 27, 2020
Another suggestion
bind(for column transformer at least).I am not convinced we should use the same verb for ColumnTransformer and the Pipeline: only ColumnTransformer takes the column selector as (primary) argument for this builder method.
NicolasHug commentedon Jul 27, 2020
pipe?GaelVaroquaux commentedon Jul 27, 2020
justmarkham commentedon Jul 27, 2020
I wanted to get a feel for how some of my teaching code would look under this proposal, so I created some examples. I started with
Pipeline, since it's a bit easier to digest:Pipeline
Option 1: This is what I use 95% of the time. It's easy to read and write, I know exactly what's in it (because of my own naming conventions), and I know exactly what the assigned step names will be.
Option 2: I don't like that I am required to name all of the steps, and I especially don't like writing or reading
([(and)])at the start and end of everyPipeline. It's too easy to make mistakes.Option 3 (no step names): I still prefer option 1, but option 3 is okay. I could get used to it.
Option 4 (step names): I like that the step names come after the objects and that the step names are optional. I don't particularly like wrapping the entire thing in parentheses. I'm not sure if I prefer option 2 or option 4.
ColumnTransformer
Option 1: This is what I use 95% of the time. It's somewhat easy to make mistakes because of the number of commas and parentheses, but I've gotten used to it.
Option 2: I don't like that I am required to name all of the transformers. I find it hard to write because of the number of parentheses, commas, and brackets.
Option 3 (no transformer names): I'm okay with this. I may still have a slight preference for option 1.
Option 4 (transformer names): I like this a bit better than option 2, especially because the transformer names are optional.
Overall assessment
I think the case for this proposal is stronger for
ColumnTransformerthan forPipeline.I share @NicolasHug's concern of having three different ways to do the same thing. So much code is already out "in the wild" that uses the existing two options that even if we strongly recommended the new way, it's fair to assume that the existing two options will continue to be used for many years to come. That may make things especially tough for new users.
Assuming this proposal was accepted, it's not clear to me whether deprecating
make_pipelineandmake_column_transformerwould make things better or worse.Method naming
I like the idea of having one method name that is used with all composite estimators, since that is less for the user to remember.
At the moment, I like add the best because:
thomasjpfan commentedon Jul 27, 2020
As a reference, users coming from
keraswould be familiar with the following:or
GaelVaroquaux commentedon Jul 27, 2020
As I mentioned in the call, we can now use dicts (and hence kwargs) which are ordered. The following synthax would be possible:
doesn't fully remove the clunckiness :)
jnothman commentedon Jul 27, 2020
jnothman commentedon Jul 27, 2020
Thanks for your feedback @justmarkham.
I'm not sure that your comparison is entirely fair, since if
is sufficient, then so is:
In some ways this is poorer to read, but it is simpler in the sense that:
Pipelineormake_pipelineis a better choice. (We could have facilitated that by makingmake_pipelineinstead a classmethodPipeline.make.)adrinjalali commentedon Jul 28, 2021
It's been a year. I'd be very happy if we revive this. Tagging @scikit-learn/core-devs since it's also related to some of the efforts related to the sample props SLEP (SLEP006).
Seems like there's a [at least weak] consensus on a way forward.
lorentzenchr commentedon Jul 28, 2021
I’d like the idea of an
addmethod, but have the same concern as @NicolasHug #16301 (comment).If we add something new, do we also want to get rid of something old?
jnothman commentedon Jul 29, 2021
That's a good point... I think that if we add a "better" API, we should change the examples/tutorial to use it. Where a method is added to control a parameter, we should note in the parameter docstring that the method is preferred. This specific proposal might involve deprecating make_pipeline etc which are convenient, but add some obscurity.