Skip to content

PolynomialFeatures always generates all combinations with degree less than the degree parameter #19627

Closed

Description

@gaspard-dv

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

  1. Xceptions commented on Mar 6, 2021

    @Xceptions

    I think this is a nice idea

  2. jnothman commented on Mar 6, 2021

    @jnothman
    Member
  3. gaspard-dv commented on Mar 6, 2021

    @gaspard-dv
    Author

    Yes!

    Indeed, if we were to put aside the importance of backward compatibility, I believe that renaming the current degree parameter to max_degree would be more precise.

    Because the doc says:

    Generate a new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree.

  4. jnothman commented on Mar 8, 2021

    @jnothman
    Member

    I think deprecating degree and replacing it with max_degree would be acceptable.

  5. gaspard-dv commented on Mar 9, 2021

    @gaspard-dv
    Author

    Ok!

    So, do you think that it would be a good idea to open a PR that:

    • deprecates degree
    • adds max_degree (that would be equivalent to the current degree)
    • adds 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?

  6. lorentzenchr commented on Mar 27, 2021

    @lorentzenchr
    Member

    Option 1

    Introduce min_degree and max_degree. What is then the meaning of interaction_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:

    If true, only interaction features are produced: features that are products of at most degree distinct input features (so not x[1] ** 2, x[0] * x[2] ** 3, etc.).

    But x[1] ** 2 are "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=True is that I get all combinations of exactly degree distinct input features:

    • degree=2: AB, AC, BC
    • degree=3: ABC
  7. gaspard-dv commented on Mar 27, 2021

    @gaspard-dv
    Author

    Yes! I've had the same thought and I agree with you @lorentzenchr.

  8. ogrisel commented on May 25, 2021

    @ogrisel
    Member

    To clarify the current behavior of interaction_only=True:

    >>> import numpy as np
    >>> import pandas as pd
    >>> from sklearn.preprocessing import PolynomialFeatures
    >>> X = pd.DataFrame(np.random.randn(100, 3), columns=list("abc"))
    >>> PolynomialFeatures(degree=3, interaction_only=True).fit(X).get_feature_names(X.columns)
    ['1', 'a', 'b', 'c', 'a b', 'a c', 'b c', 'a b c']
    >>> PolynomialFeatures(degree=3, interaction_only=False).fit(X).get_feature_names(X.columns)
    ['1', 'a', 'b', 'c', 'a^2', 'a b', 'a c', 'b^2', 'b c', 'c^2', 'a^3', 'a^2 b', 'a^2 c', 'a b^2', 'a b c', 'a c^2', 'b^3', 'b^2 c', 'b c^2', 'c^3']

    I find that this behavior is what I often want by default (to add interactions, for instance after a SplineTransform step in a pipeline) but I agree the name of the option is a bit confusing.

    Maybe we could have specific options to:

    • include or exclude the bias term
    • include or exclude the original features
    • included or exclude interaction terms (more than one features)

    and then deprecate the interaction_only option.

  9. NicolasHug commented on May 25, 2021

    @NicolasHug
    Member

    I think deprecating degree and replacing it with max_degree would be acceptable.

    Instead of deprecating degree, could we just extend it to also accept a tuple, i.e. degree=(min_degree, max_degree)?

  10. lorentzenchr commented on May 26, 2021

    @lorentzenchr
    Member

    +1 for @NicolasHug's suggestion degree=(min_degree, max_degree). It seems minimal invasive and does the job in combination with interactions_only=True (the doc of which should be corrected anyway).

  11. ogrisel commented on May 31, 2021

    @ogrisel
    Member

    +1 for @NicolasHug's suggestion degree=(min_degree, max_degree). It seems minimal invasive and does the job in combination with interactions_only=True (the doc of which should be corrected anyway).

    Fair enough.

  12. dayana123456789 commented on Mar 2, 2025

    @dayana123456789

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions