Skip to content
Merged
Changes from all commits
Commits
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
8 changes: 5 additions & 3 deletions sdks/python/apache_beam/transforms/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,11 @@ def linear_regression_no_numpy(xs, ys):
n = float(len(xs))
xbar = sum(xs) / n
ybar = sum(ys) / n
if [xs[0]] * len(xs) == xs:
if xbar == 0:
return ybar, 0
if all(xs[0] == x for x in xs):
# Simply use the mean if all values in xs are same.
return 0, ybar/xbar
return 0, ybar / xbar
b = (sum([(x - xbar) * (y - ybar) for x, y in zip(xs, ys)])
/ sum([(x - xbar)**2 for x in xs]))
a = ybar - b * xbar
Expand All @@ -295,7 +297,7 @@ def linear_regression_numpy(xs, ys):
import numpy as np
from numpy import sum
n = len(xs)
if [xs[0]] * n == xs:
if all(xs[0] == x for x in xs):
# If all values of xs are same then fallback to linear_regression_no_numpy
return _BatchSizeEstimator.linear_regression_no_numpy(xs, ys)
xs = np.asarray(xs, dtype=float)
Expand Down