Skip to content

Instantly share code, notes, and snippets.

@ajoydas
Created November 18, 2018 18:24
Show Gist options
  • Save ajoydas/bf5c4a48cf677da8028608dad20457be to your computer and use it in GitHub Desktop.
Save ajoydas/bf5c4a48cf677da8028608dad20457be to your computer and use it in GitHub Desktop.
def pocket_perceptron(weights, binary_train, binary_test):
best_weights = weights
best_accuracy = 0
count = 0
K = 100
k = 0
while True:
# print(weights)
for row in binary_train:
predicted = predict(row, weights)
# print("Class : "+ str(row[-1]) +" Predicted: "+ str(predicted))
if predicted != row[-1]:
k += 1
count = 0
if predicted == 1:
weights = sub_from_weights(weights, row)
else:
weights = add_to_weights(weights, row)
print(weights)
accuracy = test_accuracy(binary_train, weights)
if accuracy > best_accuracy:
best_weights, best_accuracy = weights, accuracy
print(accuracy)
if k == K or count == binary_train.shape[0]:
break
count += 1
if k == K or count == binary_train.shape[0]:
break
weights = best_weights
test_accuracy(binary_test, weights)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment