summaryrefslogtreecommitdiff
path: root/algorithms/perceptron.py
blob: ca1ebeede54db98ab25c02374489c188a01ff109 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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