Skip to content

Instantly share code, notes, and snippets.

@Deepayan137
Last active August 28, 2018 14:33
Show Gist options
  • Save Deepayan137/871e541b315de4333a920224a898ae09 to your computer and use it in GitHub Desktop.
Save Deepayan137/871e541b315de4333a920224a898ae09 to your computer and use it in GitHub Desktop.
import numpy as np
import random
import pdb
import os
import sys
def update(W, X_vec, n):
W_new = W + n*X_vec
return W_new
def get_weights(output_dimension):
W = np.random.randn(1, output_dimension)
return W
def augment_data(X, y):
f = lambda x,y: x*(-1) if y == -1 else x
for i in range(X.shape[0]):
X[i,:] = f(X[i,:],y[i])
return X, y
def train(X, y, wt):
errors = np.ones(y.shape)
while sum(errors) > 0:
for i in range(X.shape[0]):
if np.dot(X[i,:], np.transpose(wt)) > 0 :
errors[i] = 0
else:
wt = update(wt, X[i], 0.1)
print(wt)
if __name__ == '__main__':
X = np.array([[1, 1], [-1, -1], [2, 2], [-2, -2], [-1, 1],\
[1, -1]], dtype=np.float64)
y = np.array([1, -1, 1, -1, 1, 1], dtype=np.float64)
X, y = augment_data(X, y)
wt = get_weights(X.shape[1])
# pdb.set_trace()
train(X, y, wt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment