Skip to content

Instantly share code, notes, and snippets.

@anubhavshrimal
Created November 12, 2018 21:59
Show Gist options
  • Save anubhavshrimal/1f7e2b828ef759cb4408e5b8c4e1cd14 to your computer and use it in GitHub Desktop.
Save anubhavshrimal/1f7e2b828ef759cb4408e5b8c4e1cd14 to your computer and use it in GitHub Desktop.
import numpy as np
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 all the data points in the dataset
for i, x in enumerate(X):
y_cap = prediction(x, W, b)
# If Red point misclassified into Positive region WX + b >= 0
if y_cap - y[i] == 1:
W[0] -= learn_rate * x[0]
W[1] -= learn_rate * x[1]
b -= learn_rate
# If Blue point misclassified into Negative region WX + b < 0
elif y_cap - y[i] == -1:
W[0] += learn_rate * x[0]
W[1] += learn_rate * x[1]
b += learn_rate
return W, b
# Randomly initialise W & b
W = np.array(np.random.rand(2,1))
b = np.random.rand(1)[0] + x_max
# Run the perceptronStep for number of epochs
for i in range(num_epochs):
W, b = perceptronStep(X, y, W, b, learn_rate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment