Skip to content

Instantly share code, notes, and snippets.

@ajoydas
Created November 18, 2018 15:10
Show Gist options
  • Save ajoydas/01f998c33db852d0ff212c50eccfceff to your computer and use it in GitHub Desktop.
Save ajoydas/01f998c33db852d0ff212c50eccfceff to your computer and use it in GitHub Desktop.
w = [0.0 for i in range(len(binary_train[0])-2)]
w.append(1.0)
weights = w.copy()
# Make a prediction with weights
def predict(row, weights):
activation = 0
for i in range(len(weights)-1):
activation += weights[i] * row[i]
return 1 if activation >= 0.0 else 2
def add_to_weights(weights, row):
for i in range(len(weights)-1):
weights[i] += row[i]
return weights
def sub_from_weights(weights, row):
for i in range(len(weights)-1):
weights[i] -= row[i]
return weights
while True :
print(weights)
new_weights = weights.copy()
for row in binary_train:
predicted = predict(row, weights)
# print("Class : "+ str(row[-1]) +" Predicted: "+ str(predicted))
if predicted != row[-1]:
if predicted == 1:
new_weights = sub_from_weights(new_weights, row)
else:
new_weights = add_to_weights(new_weights, row)
if weights == new_weights:
break
weights = new_weights
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment