Skip to content

ENH add minimal degree support to PolynomialFeatures - #20250

Merged
rth merged 20 commits into
scikit-learn:mainfrom
lorentzenchr:poly_degree
Jun 26, 2021
Merged

rth merged 20 commits into
scikit-learn:mainfrom
lorentzenchr:poly_degree

Conversation

@lorentzenchr

Copy link
Copy Markdown
Member

Reference Issues/PRs

Fixes #19627.

What does this implement/fix? Explain your changes.

This adds the possibility to pass a tuple PolynomialFeatures(degree=(min_degree, max_degree)). Only terms with total degree=min_degree are produced, except for the bias term, which is still controlled by include_bias.

Examples:

  • degree=(2, 2), include_bias=True, interaction_only=False for features A, B will generate: 1, A**2, A*B, B**2
  • degree=(2, 2), include_bias=False, interaction_only=True for features A, B will generate: A*B

Any other comments?

The implementation could be more memory efficient, but I tried to make the code change not too large.

@ogrisel

ogrisel commented Jun 11, 2021

Copy link
Copy Markdown
Member

Could you please run a quick benchmark to check that this does not introduce any significant performance regression when degree is 2 or 3 for instance?

@ogrisel

ogrisel commented Jun 11, 2021

Copy link
Copy Markdown
Member

Can you please extend the tests to check the output of get_feature_names when degree is a min-max tuple?

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I did not check the code itself in details but since the tests look comprehensive enough, I trust this PR to be correct :)

Also you did not run the requested quick non-regression benchmark I requested in #20250 (comment) as far as I can see.

Just a few more tests improvement suggestions (I could not resist):

@lorentzenchr

lorentzenchr commented Jun 14, 2021 •

Copy link
Copy Markdown
Member Author

Also you did not run the requested quick non-regression benchmark I requested in #20250 (comment) as far as I can see.

That's right. I did not yet pop up on top of my priority list (and there is not really a code change from which I expect a slow down. Unfortunately, the git diff is blurred by the additional indentation.).

@lorentzenchr

Copy link
Copy Markdown
Member Author

Not the easiest to compare, nevertheless:

python benchmarks/bench_feature_expansions.py

main branch

image

this PR

image

@ogrisel

ogrisel commented Jun 15, 2021 •

Copy link
Copy Markdown
Member

Take away: tno visible performance impact as expected. Thanks for checking.

@rth rth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @lorentzenchr ! This would be quite useful indeed!

Generally LGTM. Note that there is still one comment to address in https://github.com/scikit-learn/scikit-learn/pull/20250/files#r651049827 from Olivier's review.

Comment thread sklearn/preprocessing/_polynomial.py Outdated
new_index.append(current_col)
index = new_index

Xout[:, :] = XP[:, n_XP - n_Xout :]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is probably no need to pre-allocate Xout in this case,

Suggested change
Xout[:, :] = XP[:, n_XP - n_Xout :]
Xout = XP[:, n_XP - n_Xout :].copy()

So it's complicated to compute only the columns we need in the above loop directly?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, the allocation with np.empty is only in if self.include_bias:.

So it's complicated to compute only the columns we need in the above loop directly?

Yes. I considered alternatives, but all I could come up with was more code - without clear indication of performance improvements.
This solution uses more memory than necessary, but may be faster.

@rth
rth merged commit bb2de70 into scikit-learn:main Jun 26, 2021
@lorentzenchr
lorentzenchr deleted the poly_degree branch June 26, 2021 12:02
samronsin pushed a commit to samronsin/scikit-learn that referenced this pull request Nov 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants