-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperceptronTest.py
75 lines (50 loc) · 1.94 KB
/
perceptronTest.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import perceptron as pc
import random as rd
def getRandomZero(dimension, elementMax):
negative = [0 for x in xrange(dimension / 2)]
for x in xrange(dimension / 2):
negative.append(rd.randrange(elementMax))
return negative
def getRandopmPositive(dimension, elementMax):
positive = [rd.randrange(elementMax) for x in xrange(dimension/2)]
for x in xrange(dimension/2):
positive.append(0)
return positive
def createRandomDataSet(dimension, ammount, elementMax):
trueSet = []
falseSet = []
for i in xrange(ammount / 2):
positive = getRandopmPositive(dimension, elementMax)
trueSet.append(tuple(positive))
for i in xrange(ammount / 2):
negative = getRandomZero(dimension, elementMax)
falseSet.append(tuple(negative))
trueSet = map(lambda x: (x, 1), trueSet)
falseSet = map(lambda x: (x, 0), falseSet)
trueSet.extend(falseSet)
return trueSet
def testRandomDataSet(dimension, ammount, elementMax, perceptron):
randomTest = createRandomDataSet(dimension, ammount, elementMax)
correctResultCount = 0
for test in randomTest:
if perceptron.test(test[0]) == test[1]:
correctResultCount += 1
return correctResultCount
def main():
#example of perceptron creation with meta data
dimension = 100
threshold = 0.3
learningRate = 0.1
perceptron = pc.Perceptron(dimension, threshold, learningRate)
#training with sets as [((1, 2, 3, 0, 0, 0), 1), ((0, 0, 0, 3, 3, 8), 0)]
ammount = 1000
elementMax = 256
randomTest = createRandomDataSet(dimension, ammount, elementMax)
rd.shuffle(randomTest)
perceptron.train(randomTest, dimension)
testAmmount = 1000
correctnes = testRandomDataSet(dimension, testAmmount, elementMax, perceptron)
print "correctness :", correctnes, "over :", testAmmount
perceptron.saveWeights("test")
perceptron = pc.Perceptron.perceptronWithFilename("test")
main()