Skip to content

Instantly share code, notes, and snippets.

@jainakshansh
Last active December 22, 2018 18:26
Show Gist options
  • Save jainakshansh/5ba5d22b4e3b89fd161588ad3f1c43cf to your computer and use it in GitHub Desktop.
Save jainakshansh/5ba5d22b4e3b89fd161588ad3f1c43cf to your computer and use it in GitHub Desktop.
The Perceptron Algorithm for Linear Classification Model - Facebook Udacity PyTorch Challenge
import numpy as np
# What does numpy.random.seed() do? - https://stackoverflow.com/questions/21494489/what-does-numpy-random-seed0-do
np.random.seed(42)
def stepFunction(t):
if t >= 0:
return 1
return 0
def prediction(X, W, b):
return stepFunction((np.matmul(X, W) + b)[0])
def perceptronStep(X, y, W, b, learn_rate=0.01):
for i in range(len(X)):
y_hat = prediction(X[i], W, b)
if y[i] - y_hat == 1:
W[0] += X[i][0] * learn_rate
W[1] += X[i][1] * learn_rate
b += learn_rate
elif y[i] - y_hat == -1:
W[0] -= X[i][0] * learn_rate
W[1] -= X[i][1] * learn_rate
b -= learn_rate
return W, b
def trainPerceptronAlgorithm(X, y, learn_rate=0.01, num_epochs=25):
x_min, x_max = min(X.T[0]), max(X.T[0])
y_min, y_max = min(X.T[1]), max(X.T[1])
W = np.array(np.random.rand(2, 1))
b = np.random.rand(1)[0] + x_max
boundary_lines = []
for i in range(num_epochs):
W, b = perceptronStep(X, y, W, b, learn_rate)
boundary_lines.append((-W[0] / W[1], -b / W[1]))
return boundary_lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment