Last active
February 14, 2019 10:05
-
-
Save tyliec/2e98c1812e8c3b29b57297492d8dc94a to your computer and use it in GitHub Desktop.
python3 please
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import random | |
import matplotlib.pyplot as plt | |
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] | |
return int(summation > 0) | |
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) | |
# Lists to store our training inputs & labels | |
training_inputs, labels = [], [] | |
# random.seed("TRAINING") # For testing purposes; keeps our data the same while running through multiple iterations | |
num_training_inputs = 1000 # Amount of training inputs to generate | |
xMin, xMax = (0, 100) | |
yMin, yMax = (2 * xMin, 2 * xMax) | |
def calc_ans(x, y): | |
return int(y < 2*x) | |
# Generating training inputs with our labels (0, 1) being whether y < 2x | |
for _ in range(0, num_training_inputs): | |
rand_x = random.randint(xMin, xMax) | |
rand_y = random.randint(yMin, yMax) | |
training_inputs.append(np.array([rand_x, rand_y])) | |
labels.append(calc_ans(rand_x, rand_y)) | |
# Initializing and training our perceptron | |
perceptron = Perceptron(2) | |
perceptron.train(training_inputs, np.array(labels)) | |
# Plotting our decision boundary line | |
plt.plot([xMin, xMax], [yMin, yMax], linestyle='-', linewidth=2, color='#E0115F') | |
# Plotting our training inputs | |
training_inputs = np.array(training_inputs) | |
plt.scatter(training_inputs[:,0], training_inputs[:,1], c=np.array(labels)) | |
# plt.show() | |
# Testing our perceptron | |
def unit_test(): | |
num_test_inputs = 100 | |
num_correct, num_wrong = 0, 0 | |
for _ in range(0, num_test_inputs): | |
rand_x = random.randint(xMin, xMax) | |
rand_y = random.randint(yMin, yMax) | |
perceptron_output = perceptron.predict(np.array([rand_x, rand_y])) | |
expected_output = calc_ans(rand_x, rand_y) | |
if perceptron_output == expected_output: | |
num_correct += 1 | |
else: | |
num_wrong += 1 | |
# Print out summary | |
print(f"Number of training inputs {num_training_inputs}.") | |
print(f"Number of test cases {num_test_inputs}.") | |
print(f"Percent of test cases correct: {(num_correct / (num_correct + num_wrong)) * 100}%.") | |
print(f"Percent of test cases incorrect: {(num_wrong / (num_correct + num_wrong)) * 100}%.") | |
unit_test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment