Created
August 4, 2021 19:17
-
-
Save vinimonteiro/33a8af9b8b93ddec0fe3f1c67fbcc190 to your computer and use it in GitHub Desktop.
Linear regression algorithm from Pragramatic programmers
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 numpy as np | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
def predict(X, w, b): | |
return X * w + b | |
def loss(X, Y, w, b): | |
return np.average((predict(X, w, b) - Y) ** 2) | |
def train(X, Y, iterations, lr): | |
w = b = 0 | |
for i in range(iterations): | |
current_loss = loss(X, Y, w, b) | |
print("Iteration %4d => Loss: %.6f" % (i, current_loss)) | |
if loss(X, Y, w + lr, b) < current_loss: | |
w += lr | |
elif loss(X, Y, w - lr, b) < current_loss: | |
w -= lr | |
elif loss(X, Y, w, b + lr) < current_loss: | |
b += lr | |
elif loss(X, Y, w, b - lr) < current_loss: | |
b -= lr | |
else: | |
return w, b | |
raise Exception("Couldn't converge within %d iterations" % iterations) | |
X, Y = np.loadtxt("pizza.txt", skiprows=1, unpack=True) | |
w, b = train(X, Y, iterations=10000, lr=0.01) | |
print("\nw=%.3f, b=%.3f" % (w, b)) | |
print("Prediction: x=%d => y=%.2f" % (20, predict(20, w, b))) | |
sns.set() | |
plt.axis([0, 50, 0, 50]) | |
plt.xticks(fontsize=15) | |
plt.yticks(fontsize=15) | |
plt.xlabel("Reservations", fontsize=30) | |
plt.ylabel("Pizzas", fontsize=30) | |
plt.plot(X, Y, "bo") | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment