Created
September 19, 2021 05:15
-
-
Save thomasnield/ffa10fa845c548e8687203cc5e2c0e76 to your computer and use it in GitHub Desktop.
Linear Regression with SymPy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from sympy import * | |
# Import points from CSV | |
points = list(pd.read_csv("https://bit.ly/2KF29Bd").itertuples()) | |
m, b, i, n = symbols('m b i n') | |
x, y = symbols('x y', cls=Function) | |
sum_of_squares = Sum((m*x(i) + b - y(i)) ** 2, (i, 0, n)) | |
d_m = diff(sum_of_squares, m).subs(n, len(points) - 1).doit() \ | |
.replace(x, lambda i: points[i].x) \ | |
.replace(y, lambda i: points[i].y) | |
d_b = diff(sum_of_squares, b).subs(n, len(points) - 1).doit() \ | |
.replace(x, lambda i: points[i].x) \ | |
.replace(y, lambda i: points[i].y) | |
# compile using lambdify for faster computation | |
d_m = lambdify([m, b], d_m) | |
d_b = lambdify([m, b], d_b) | |
# Building the model | |
m = 0.0 | |
b = 0.0 | |
# The learning Rate | |
L = .001 | |
# The number of iterations | |
iterations = 100_000 | |
# Perform Gradient Descent | |
for i in range(iterations): | |
# update m and b | |
m -= d_m(m,b) * L | |
b -= d_b(m,b) * L | |
print("y = {0}x + {1}".format(m, b)) | |
# y = 1.939393939393954x + 4.733333333333231 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment