# Linear regression with no external library

```
import matplotlib.pyplot as plt
# Adapted from "Finite mathematics and applied calculus", 6ed, page 99
def slope_intercept(points):
n = len(points)
s = sum([x*y for x, y in points])
xs = sum([xy[0] for xy in points])
ys = sum([xy[1] for xy in points])
sx2 = sum([xy[0]**2 for xy in points])
m = (n*s - xs*ys) / (n*sx2 - xs**2)
b = (ys - m*xs) / n
return m, b
points = [
[12, 8],
[20, 4],
[17, 14],
[26, 18],
[35, 21],
[25, 22],
[35, 40],
[28, 22],
[45, 35],
[37, 26],
[41, 38],
[55, 19],
]
px, py = [p[0] for p in points], [p[1] for p in points]
m, b = slope_intercept(points)
xs = min(px), max(px)
ys = tuple(map(lambda x: m*x + b, xs))
plt.plot(px, py, '.', markersize=8, color='grey')
plt.plot(xs, ys, '-', lw=2, color='red')
plt.tight_layout()
plt.show()
```