From 64286d6aa65ed2071fcf529a13a795b601937b46 Mon Sep 17 00:00:00 2001 From: George Lacey <490796@hull.ac.uk> Date: Fri, 10 Feb 2017 12:12:16 +0000 Subject: [PATCH 1/3] Create Dataset class --- Dataset.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Dataset.py diff --git a/Dataset.py b/Dataset.py new file mode 100644 index 0000000..864ee5b --- /dev/null +++ b/Dataset.py @@ -0,0 +1,16 @@ +from Data import Data + + +class Dataset(object): + + def __init__(self, p_input, p_target): + self.inputs = {} + + for index, item in enumerate(p_input): + input1 = float(p_input[index].split(',')[0].strip()) + input2 = float(p_input[index].split(',')[1].strip()) + if p_target[index] == "1": + output = 1 + else: + output = -1 + self.inputs[index] = Data(input1, input2, output) From d91911691afddba8a3aba0e2766259135446a8cf Mon Sep 17 00:00:00 2001 From: George Lacey <490796@hull.ac.uk> Date: Fri, 10 Feb 2017 12:12:44 +0000 Subject: [PATCH 2/3] Implement guess method --- Perceptron.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Perceptron.py b/Perceptron.py index 327d17e..f499cc0 100644 --- a/Perceptron.py +++ b/Perceptron.py @@ -27,6 +27,11 @@ class Perceptron(object): print("Total = %f" % input) if input > 0: - return True + return 1 else: - return False + return -1 + + def guess(self, p_desired): + prediction = float(self.activation()) + return p_desired - prediction + From e4accc5927f73fa34444ed0f4ff9d838b15bf724 Mon Sep 17 00:00:00 2001 From: George Lacey Date: Fri, 10 Feb 2017 12:16:53 +0000 Subject: [PATCH 3/3] Add Main --- Main.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Main.py b/Main.py index e69de29..7bcc4db 100644 --- a/Main.py +++ b/Main.py @@ -0,0 +1,16 @@ +from Perceptron import Perceptron +from random import Random +from Dataset import Dataset + +rand = Random() +data = Dataset(open("input.txt").read().split('\n'), open("target.txt").read().split('\n')) + + +for key in data.inputs: + i = data.inputs + print("v1: ", i[key].value1, "v2: ", i[key].value2, "target: ", i[key].target) + p = Perceptron() + p.add_input("1", i[key].value1, rand.uniform(-1, 1)) + p.add_input("2", i[key].value2, rand.uniform(-1, 1)) + p.add_input("bias", 1, rand.uniform(-1, 1)) + print("%f" % p.guess(i[key].target))