diff options
author | Patrick Simianer <p@simianer.de> | 2014-06-15 03:24:33 +0200 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2014-06-15 03:24:33 +0200 |
commit | cf3a29feb5887344b6633ead1b4b6d5657a15a4b (patch) | |
tree | f1149508f7305a48dba0226699dfafdd68d81969 /algorithms/perceptron.py | |
parent | 5ddc763ab9953eebdaf78af4eb72288d7955b310 (diff) |
old stuff: algorithms
Diffstat (limited to 'algorithms/perceptron.py')
-rw-r--r-- | algorithms/perceptron.py | 51 |
1 files changed, 51 insertions, 0 deletions
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 + |