Skip to content

Instantly share code, notes, and snippets.

@tyliec
Forked from Thomascountz/perceptron.py
Created February 7, 2019 22:53
Show Gist options
  • Save tyliec/ff75cc373c4f2a5bdc4c2029405bb410 to your computer and use it in GitHub Desktop.
Save tyliec/ff75cc373c4f2a5bdc4c2029405bb410 to your computer and use it in GitHub Desktop.
Perceptron in Python v.1
import numpy as np
class Perceptron(object):
def __init__(self, no_of_inputs, threshold=100, learning_rate=0.01):
self.threshold = threshold
self.learning_rate = learning_rate
self.weights = np.zeros(no_of_inputs + 1)
def predict(self, inputs):
summation = np.dot(inputs, self.weights[1:]) + self.weights[0]
if summation > 0:
activation = 1
else:
activation = 0
return activation
def train(self, training_inputs, labels):
for _ in range(self.threshold):
for inputs, label in zip(training_inputs, labels):
prediction = self.predict(inputs)
self.weights[1:] += self.learning_rate * (label - prediction) * inputs
self.weights[0] += self.learning_rate * (label - prediction)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment