Implementing Linear Regression

Learn how to implement linear regression using Python’s math library.

  1. Calculate mean:
def mean(values):
    return sum(values) / float(len(values))
  1. Calculate variance:
def variance(values, mean):
    return sum((x-mean)**2 for x in values)
  1. Calculate covariance:
def covariance(x, mean_x, y, mean_y):
    return sum((x[i]-mean_x)*(y[i]-mean_y) for i in range(len(x)))

Read more: Linear Regression