Skip to content

Instantly share code, notes, and snippets.

@grohith327
Last active January 11, 2020 18:32
Show Gist options
  • Save grohith327/6f272908927c6e4200640caa0ea99d19 to your computer and use it in GitHub Desktop.
Save grohith327/6f272908927c6e4200640caa0ea99d19 to your computer and use it in GitHub Desktop.
## Support Vector Machine
import numpy as np
train_f1 = x_train[:,0]
train_f2 = x_train[:,1]
train_f1 = train_f1.reshape(90,1)
train_f2 = train_f2.reshape(90,1)
w1 = np.zeros((90,1))
w2 = np.zeros((90,1))
epochs = 1
alpha = 0.0001
while(epochs < 10000):
y = w1 * train_f1 + w2 * train_f2
prod = y * y_train
print(epochs)
count = 0
for val in prod:
if(val >= 1):
cost = 0
w1 = w1 - alpha * (2 * 1/epochs * w1)
w2 = w2 - alpha * (2 * 1/epochs * w2)
else:
cost = 1 - val
w1 = w1 + alpha * (train_f1[count] * y_train[count] - 2 * 1/epochs * w1)
w2 = w2 + alpha * (train_f2[count] * y_train[count] - 2 * 1/epochs * w2)
count += 1
epochs += 1
@bmuftic1
Copy link

bmuftic1 commented Aug 27, 2019

What is cost used for?
And wouldn't lines 29 and 30 produce an error, since train_f1[count] and y_train[count] are numbers, whereas 2 * 1/epochs * w1 is a vector?

Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment