Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GH-102670: Use sumprod() to simplify, speed up, and improve accuracy of statistics functions#102649

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
rhettinger merged 11 commits intopython:mainfromrhettinger:statistics_with_sumprod
Mar 14, 2023
Merged
Changes from1 commit
Commits
Show all changes
11 commits
Select commitHold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Use sumprod() in linear_regression().
  • Loading branch information
@rhettinger
rhettinger committedMar 13, 2023
commit16b2745301a6212dd9eb0cce646255fc35bccb9d
10 changes: 6 additions & 4 deletionsLib/statistics.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1134,13 +1134,15 @@ def linear_regression(x, y, /, *, proportional=False):
if n < 2:
raise StatisticsError('linear regression requires at least two data points')
if proportional:
sxy =fsum(xi * yi for xi, yi in zip(x, y))
sxx =fsum(xi * xi for xi in x)
sxy =sumprod(x, y)
sxx =sumprod(x, x)
else:
xbar = fsum(x) / n
ybar = fsum(y) / n
sxy = fsum((xi - xbar) * (yi - ybar) for xi, yi in zip(x, y))
sxx = fsum((d := xi - xbar) * d for xi in x)
centered_x = [xi - xbar for xi in x]
centered_y = (yi - ybar for yi in y)
sxy = sumprod(centered_x, centered_y)
sxx = sumprod(centered_x, centered_x)
try:
slope = sxy / sxx # equivalent to: covariance(x, y) / variance(x)
except ZeroDivisionError:
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp