Skip to content

Instantly share code, notes, and snippets.

@79man
Created November 9, 2023 10:52
Show Gist options
  • Save 79man/e6fa620ae57e347f26598134994df665 to your computer and use it in GitHub Desktop.
Save 79man/e6fa620ae57e347f26598134994df665 to your computer and use it in GitHub Desktop.
Machine Learning Derivatives
import numpy as np
def predict(X, w):
return X * w
def loss (X, Y, w):
return np.average ((predict(X, w) - Y) ** 2)
def gradient(X, Y, w):
w_gradient = 2 * np.average (X * (predict(X, w) - Y))
return w_gradient
def train(X, Y, iterations, lr=0.00001):
w = 0
for i in range(iterations):
l = loss (X, Y, w)
w_gradient = gradient(X, Y, w)
w = w - w_gradient *lr
# Update the height coefficient by multiplying the gradient and learning rate (lr).
return w
X = np.array([ 161, 163, 178, 157, 188, 161])
Y = np.array([47.81, 54.92, 53.99, 45.84, 51.92, 58.01])
print("iterations,\tcoeff")
for its in range(5,155,5):
height_coeff = train(X, Y, iterations=its)
print('{},\t\t{}'.format(its, height_coeff))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment