Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate duplicated calculations and unnecessary work for linear regression #25922

Merged
merged 10 commits into from May 6, 2021

Conversation

@rhettinger
Copy link
Contributor

@rhettinger rhettinger commented May 5, 2021

The current code, while pretty, does repeated calculations and unnecessary work:

  • covariance() and variance() both divide by n - 1 which is thrown away in the slope calculation. This also causes two unnecessary roundings.

  • covariance(x,y) and variance(x) both compute fmean(x). This doesn't need to be done twice.

  • variance(x) uses the extremely slow internal _ss(), _sum(), and _convert() functions whose purpose is to preserve type information. However, that type information is thrown away by linear_regression(x, y) which always returns a pair of floats:

    >>> from statistics import linear_regression
    >>> from fractions import Fraction as F
    >>> linear_regression([F(1,2), F(2,3)], [F(5,7), F(8,9)])
    LinearRegression(intercept=0.19047619047619047, slope=1.0476190476190477)
  • the intercept calculation makes two more redundant fmean() calls that are unnecessary.

  • The inlined code makes the actual calculation more clear. It matches this typical presentation: slope = s_{x,y} / s^2_x

rhettinger added 10 commits Mar 16, 2021
Update to 15 March
.
Merge branch 'master' of github.com:python/cpython
.
Merge branch 'master' of github.com:python/cpython
.
Merge branch 'master' of github.com:python/cpython
.
Merge branch 'master' of github.com:python/cpython
Merge branch 'main' of github.com:python/cpython into main
@rhettinger rhettinger requested a review from pablogsal May 5, 2021
@rhettinger rhettinger changed the title Inline the calculations for linear regression Eliminate duplicated calculations and unnecessary work for linear regression May 5, 2021
Copy link
Member

@pablogsal pablogsal left a comment

LGTM!

x, y = regressor, dependent_variable
xbar = fsum(x) / n
ybar = fsum(y) / n
sxy = fsum((xi - xbar) * (yi - ybar) for xi, yi in zip(x, y))

This comment has been minimized.

@pablogsal

pablogsal May 6, 2021
Member

Question: isn't the generator + zip going to make it slightly slower?

This comment has been minimized.

@rhettinger

rhettinger May 6, 2021
Author Contributor

That was an existing line take from covariance(). I think it is the fastest way the run this computation.

@rhettinger rhettinger merged commit 55b78ce into python:main May 6, 2021
10 of 11 checks passed
10 of 11 checks passed
@github-actions
Check for source changes
Details
@github-actions
Check if generated files are up to date
Details
@github-actions
Windows (x86)
Details
@github-actions
Windows (x64)
Details
@github-actions
macOS
Details
@github-actions
Ubuntu
Details
@github-actions
Ubuntu SSL tests with OpenSSL
Details
Azure Pipelines PR #20210505.27 failed
Details
@travis-ci
Travis CI - Pull Request Build Passed
Details
@bedevere-bot
bedevere/issue-number Issue report skipped
@bedevere-bot
bedevere/news "skip news" label found
@miss-islington
Copy link
Contributor

@miss-islington miss-islington commented May 6, 2021

Thanks @rhettinger for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10.
🐍🍒🤖

@miss-islington
Copy link
Contributor

@miss-islington miss-islington commented May 6, 2021

Sorry @rhettinger, I had trouble checking out the 3.10 backport branch.
Please backport using cherry_picker on command line.
cherry_picker 55b78ce3c4e23abe4f27bf16d7968f8851532e47 3.10

@miss-islington
Copy link
Contributor

@miss-islington miss-islington commented May 6, 2021

Thanks @rhettinger for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10.
🐍🍒🤖

@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented May 6, 2021

GH-25945 is a backport of this pull request to the 3.10 branch.

miss-islington added a commit to miss-islington/cpython that referenced this pull request May 6, 2021
…ression (pythonGH-25922)

(cherry picked from commit 55b78ce)

Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
rhettinger pushed a commit that referenced this pull request May 6, 2021
…ression (GH-25922) (GH-25945)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants