Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thomasnield/2defb663fc83c774b70e48e7703acb17 to your computer and use it in GitHub Desktop.
Save thomasnield/2defb663fc83c774b70e48e7703acb17 to your computer and use it in GitHub Desktop.
sympy_simple_linear_regression_derivatives.py
from sympy import *
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)
d_b = diff(sum_of_squares, b)
print(d_m)
print(d_b)
# OUTPUTS
# Sum(2*(b + m*x(i) - y(i))*x(i), (i, 0, n))
# Sum(2*b + 2*m*x(i) - 2*y(i), (i, 0, n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment