From cf3a29feb5887344b6633ead1b4b6d5657a15a4b Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Sun, 15 Jun 2014 03:24:33 +0200 Subject: old stuff: algorithms --- algorithms/perceptron.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 algorithms/perceptron.py (limited to 'algorithms/perceptron.py') diff --git a/algorithms/perceptron.py b/algorithms/perceptron.py new file mode 100644 index 0000000..ca1ebee --- /dev/null +++ b/algorithms/perceptron.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python2 + +from operator import add, mul + + +def pointwise(op, x, y): + return [op(a, b) for (a, b) in zip(x, y)] + +def scalar_product(x, y): + """Scalar product.""" + return sum(pointwise(mul, x, y)) + +def scalar_multiplication(s, v): + """Scalar multiplication.""" + return [s*i for i in v] + +# data extracted from corpus +x = {1 : [ 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 ], + 2 : [ 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ], + 3 : [ 0 , 1 , 0 , 1 , 1 , 0 , 0 , 0 , 0 ], + 4 : [ 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 ], + 5 : [ 0 , 0 , 0 , 4 , 0 , 0 , 0 , 0 , 1 ], + 6 : [ 0 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 0 ], + 7 : [ 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 ], + 8 : [ 0 , 0 , 0 , 0 , 0 , 2 , 0 , 0 , 0 ], + 9 : [ 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 ], + 10 : [ 0 , 0 , 0 , 0 , 0 , 2 , 0 , 0 , 2 ]} +y = {1:1, 2:-1, 3:-1, 4:1, 5:1, 6:1, 7:1, 8:-1, 9:1, 10:-1} + +# learning rate +alpha = 0.5 +# initialize parameters +w = [0,0,0,0,0,0,0,0,0] +rho = 0 +turn = 1 +errors = True + +# perceptron algorithm +while errors: + print "\nTurn", turn + turn += 1 + errors = False + for i in x.keys(): + print "i =", i + trigger = y[i] * (scalar_product (w, x[i]) + rho) + if trigger <= 0: + errors = True + w = pointwise(add, w, scalar_multiplication(alpha*y[i], x[i])) + rho += alpha*y[i] + print w, rho + -- cgit v1.2.3