mirror of
https://github.com/python/cpython.git
synced 2025-10-14 18:59:46 +00:00
Eliminate duplicated calculations and unnecessary work for linear regression (GH-25922)
This commit is contained in:
parent
e8525567dd
commit
55b78ce3c4
1 changed files with 7 additions and 2 deletions
|
@ -952,11 +952,16 @@ def linear_regression(regressor, dependent_variable, /):
|
||||||
raise StatisticsError('linear regression requires that both inputs have same number of data points')
|
raise StatisticsError('linear regression requires that both inputs have same number of data points')
|
||||||
if n < 2:
|
if n < 2:
|
||||||
raise StatisticsError('linear regression requires at least two data points')
|
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:
|
try:
|
||||||
slope = covariance(regressor, dependent_variable) / variance(regressor)
|
slope = sxy / s2x
|
||||||
except ZeroDivisionError:
|
except ZeroDivisionError:
|
||||||
raise StatisticsError('regressor is constant')
|
raise StatisticsError('regressor is constant')
|
||||||
intercept = fmean(dependent_variable) - slope * fmean(regressor)
|
intercept = ybar - slope * xbar
|
||||||
return LinearRegression(intercept=intercept, slope=slope)
|
return LinearRegression(intercept=intercept, slope=slope)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue