Repository navigation
Description
Problem
Currently, PolynomialFeatures states:
Generate a new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree.
The problem with that is that we often only need the combinations of the features with degree strictly equal to the specified degree.
For instance:
Let's say that we set interaction_only=True.
Let's say that we have 3 input features: A, B, C.
PolynomialFeatures will generate 7 features: 1, A, B, C, AB, AC, BC.
But we may want to generate only AB, AC, BC.
Background
According to my humble experience, PolynomialFeatures isn't flexible enough to be useful in many practical cases.
Related issue:
A few days ago, this issue was opened to suggest similar changes. The issue suggests to add a combinations parameter that would allow the user to specify manually the combinations that will generate the polynomial features.
In my opinion, although this idea is more powerful than what I suggest in this issue, in many cases it might be tedious for the user to generate the combinations.
So, I believe that if the combinations parameter is added, it would be great to add a helper function to facilitate the generation of the combinations.
Proposed solution
Flexible solution
Let the user pass a list as degree parameter. For instance, the user could do: PolynomialFeatures(degree=[2, 3], interaction_only=True) to generate the combinations of the features with degree strictly equal to 2 or 3.
The problem I see with this idea is that passing degree=[2] would not have the same behaviour as the current behaviour when we use degree=2. So, it might be confusing for the user. Perhaps we could add a degrees parameter and leave degree unchanged? I'm open to ideas regarding this small issue.
Minimalist solution
Currently, we call PolynomialFeatures._combinations which generates the combinations starting from:
- degree 0 if
include_bias = True - and degree 1 if
include_bias = False.
Instead of include_bias, we could use a new min_degree parameter which would be an integer less or equal to degree.
i.e:
def _combinations(n_features, degree, interaction_only, include_bias):
...
start = int(not include_bias)
...would become something like:
def _combinations(n_features, degree, interaction_only, min_degree=0):
...
start = min_degree
...So, to solve the example given in the "Problem" paragraph, we would simply call:
PolynomialFeatures(degree=2, interaction_only=True, min_degree=2)Alternatives
To achieve the same goal, I see two possible options:
- We could filter the output columns to remove the generated features that we don't need, but this is a waste of memory and computing power.
- We could override PolynomialFeatures locally to change the behaviour of
_combinations, or implement the custom preprocessor from scratch.
I hope that my explanations make sense.
Thank you for your time,
Activity
Xceptions commentedon Mar 6, 2021
I think this is a nice idea
jnothman commentedon Mar 6, 2021
gaspard-dv commentedon Mar 6, 2021
Yes!
Indeed, if we were to put aside the importance of backward compatibility, I believe that renaming the current
degreeparameter tomax_degreewould be more precise.Because the doc says:
jnothman commentedon Mar 8, 2021
I think deprecating
degreeand replacing it withmax_degreewould be acceptable.gaspard-dv commentedon Mar 9, 2021
Ok!
So, do you think that it would be a good idea to open a PR that:
degreemax_degree(that would be equivalent to the currentdegree)min_degree?
If so, I can do it.
Or do you think that there is a different/flexible/better solution that we should consider instead?
lorentzenchr commentedon Mar 27, 2021
Option 1
Introduce
min_degreeandmax_degree. What is then the meaning ofinteraction_only=True?Option 2
Clearify / Change behavior of current
interaction_only=True.I always find and found the behavior of
PolynomialFeatures(degree=2, interaction_only=True)very misleading. The doc states:But
x[1] ** 2are "at most" 2 distinct input features, in fact it's only one and should be included. The description is inconsistent. On top of that, it always returns the original input as well (A, B, C, AB, AC, BC) instead of just (AB, AC, BC).My naiv expectation for
interaction_only=Trueis that I get all combinations of exactlydegreedistinct input features:degree=2: AB, AC, BCdegree=3: ABCgaspard-dv commentedon Mar 27, 2021
Yes! I've had the same thought and I agree with you @lorentzenchr.
ogrisel commentedon May 25, 2021
To clarify the current behavior of
interaction_only=True:I find that this behavior is what I often want by default (to add interactions, for instance after a
SplineTransformstep in a pipeline) but I agree the name of the option is a bit confusing.Maybe we could have specific options to:
and then deprecate the
interaction_onlyoption.NicolasHug commentedon May 25, 2021
Instead of deprecating
degree, could we just extend it to also accept a tuple, i.e.degree=(min_degree, max_degree)?lorentzenchr commentedon May 26, 2021
+1 for @NicolasHug's suggestion
degree=(min_degree, max_degree). It seems minimal invasive and does the job in combination withinteractions_only=True(the doc of which should be corrected anyway).ogrisel commentedon May 31, 2021
Fair enough.
dayana123456789 commentedon Mar 2, 2025
Using polynomial features does it improves the model especially if I use tree based model
There will be overfitting but still for creating only interaction terms.