[Python-checkins] Eliminate duplicated calculations and unnecessary work for linear regression (GH-25922)
rhettinger
webhook-mailer at python.org
Thu May 6 10:43:23 EDT 2021
https://github.com/python/cpython/commit/55b78ce3c4e23abe4f27bf16d7968f8851532e47
commit: 55b78ce3c4e23abe4f27bf16d7968f8851532e47
branch: main
author: Raymond Hettinger <rhettinger at users.noreply.github.com>
committer: rhettinger <rhettinger at users.noreply.github.com>
date: 2021-05-06T07:43:13-07:00
summary:
Eliminate duplicated calculations and unnecessary work for linear regression (GH-25922)
files:
M Lib/statistics.py
diff --git a/Lib/statistics.py b/Lib/statistics.py
index edb11c868c1c8..db8c581068b7d 100644
--- a/Lib/statistics.py
+++ b/Lib/statistics.py
@@ -952,11 +952,16 @@ def linear_regression(regressor, dependent_variable, /):
raise StatisticsError('linear regression requires that both inputs have same number of data points')
if n < 2:
raise StatisticsError('linear regression requires at least two data points')
+ 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))
+ s2x = fsum((xi - xbar) ** 2.0 for xi in x)
try:
- slope = covariance(regressor, dependent_variable) / variance(regressor)
+ slope = sxy / s2x
except ZeroDivisionError:
raise StatisticsError('regressor is constant')
- intercept = fmean(dependent_variable) - slope * fmean(regressor)
+ intercept = ybar - slope * xbar
return LinearRegression(intercept=intercept, slope=slope)
More information about the Python-checkins
mailing list