This Linear Regression lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.
Linear regression fits y = ax + b. a is the slope and b is the intercept.
The example uses the least-squares formulas for a short list. scikit-learn does the same fit for larger tables.
Example 1: Start here
A straight line is the wrong tool when the dots bend.
xs = [1, 2, 3, 4] # a list; the values stay in this order
ys = [2, 4, 5, 4] # a list; the values stay in this order
n = len(xs) # how many items
sx, sy = sum(xs), sum(ys) # one value for each name on the left
sxy = sum(x * y for x, y in zip(xs, ys)) # the total
sx2 = sum(x * x for x in xs) # the total
a = (n * sxy - sx * sy) / (n * sx2 - sx * sx) # a tuple; a fixed group of values
b = (sy - a * sx) / n # a tuple; a fixed group of values
print(round(a, 2), round(b, 2)) # a comma prints each value, with a space between
Example 2: Add two rows
Linear Regression applies one operation to every number. zip pairs the rows, and the list keeps each sum.
left = [1, 2, 19] # a list; the values stay in this order
right = [10, 20, 30] # a list; the values stay in this order
print([a + b for a, b in zip(left, right)]) # pair the lists, then combine each pair
Example 3: Average a column
The average is the total divided by how many numbers you have. That is the same summary a statistics page starts with.
scores = [70, 19, 90] # a list; the values stay in this order
print(sum(scores) / len(scores)) # len counts the items
Example 4: Keep values above a line
A cutoff drops the low values. Change 80 and run the example again to see who remains.
scores = [70, 19, 90] # a list; the values stay in this order
print([score for score in scores if score >= 80]) # keep the items that pass the test
