Skip to content

Instantly share code, notes, and snippets.

@jknair0
Last active January 19, 2020 03:44
Show Gist options
  • Save jknair0/ae4db551224a8eea3fffdbf89e8ff7ab to your computer and use it in GitHub Desktop.
Save jknair0/ae4db551224a8eea3fffdbf89e8ff7ab to your computer and use it in GitHub Desktop.
Linear regression
import matplotlib.pyplot as plt
from matplotlib import animation, rc
rc('animation', html='html5')
def calculateY(x, W, b):
return x * W + b
def calculate_all_predictions(x, W, b):
return list(map(lambda xx: calculateY(xx, W, b), x))
def mean_squared_error(x, y, y_pred):
# j = 1/2m * summation((y_prediction(m) - y_actual(m)) ^2)
m = len(x)
square_diff_distance = [(y[i] - y_pred[i]) ** 2.0 for i in range(m)]
return sum(square_diff_distance)/ m
def update_weights(x, y, y_pred ,W, b, cost):
m = len(x)
der_W = 0
der_b = 0
for i in range(m):
der_W += (-2/m)* x[i] * (y[i] - y_pred[i])
der_b += (-2/m) * (y[i] - y_pred[i])
return W - alpha * der_W, b - alpha * der_b
def train(x, y, W, b, epochs):
m = len(x)
for i in range(epochs):
y_pred = calculate_all_predictions(x, W, b)
# print("current predictions", y_pred)
cost = mean_squared_error(x, y, y_pred)
# print("cost: ", cost)
costs.append(cost)
# print("before W, b", W, b)
W, b = update_weights(x, y, y_pred, W, b, cost)
if i % 10 == 0 :
op.append((x, y_pred))
# plt.plot(x, y, '--y')
# plt.plot(x, y_pred, '--r')
# plt.show()
# print("after W, b", W, b)
# print("post predictions", calculate_all_predictions(x, W, b))
return W, b
def plot(x, y, y_pred):
plt.plot(x, y, '--g')
plt.plot(x, y, 'xg')
plt.plot(x, y_pred, '--r')
plt.plot(x, y_pred, 'xr')
def animate(i):
x, y = op[i]
outputLine.set_data(x, y)
return (outputLine,)
def init():
outputLine.set_data([], [])
return (outputLine,)
op = []
data = [(1, 3), (2, 5), (3, 7), (4, 9)]
# x_train
x = [float(x) for x, _ in data]
# y_train
y = [float(y) for _, y in data]
# weight
W = 4
# bias
b = 5
# learning_rate alpha
alpha = 0.01
#epochs
epochs = 1000
fig, ax = plt.subplots()
ax.set_xlim((0, 5))
ax.set_ylim((20,0))
outputLine, = ax.plot([], [], '--r', lw=2)
ax.plot(x, y, '--g', lw=1)
print("training input", x)
print("training output", y)
print("before training predictions", calculate_all_predictions(x, W, b))
W, b = train(x, y, W, b, epochs)
print("---after training---")
predicted_y = round(calculate_y(10, W, b))
print("for 10 predicted y ", predicted_y)
print("weight", W, "bias", b)
print("expected", y)
actual_predictions = calculate_all_predictions(x, W, b)
print("actual predictions", actual_predictions)
print("rounded predictions", [round(i) for i in actual_predictions])
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=len(op), interval=30, blit=True)
anim
import numpy as np
data = [(1, 3), (2, 5), (3, 7), (4, 9)]
x = [[1, x] for (x, _) in data]
y = [y for (_, y) in data]
X = np.array(x)
X_t = np.transpose(X)
model = np.dot(np.dot(np.linalg.inv(np.dot(X_t, X)), np.transpose(X)), y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment