-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperceptron.py
More file actions
74 lines (64 loc) · 2.53 KB
/
perceptron.py
File metadata and controls
74 lines (64 loc) · 2.53 KB
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
#digit images are 28x28 pixels
import operator
import imageload
class PerceptronNetwork(object):
def __init__(self, f, y):
self.features = [0.0] * f
self.outputs = y
self.weights = [[0]*f]*len(y)
def score(self, input, output):
w = self.weights[output]
y = 0.0
for i in range(0, len(w)):
y = y + (w[i]*input[i])
return y
def train(self, imageWidth, imageHeight, imagesPath, labelsPath, percentage=1.0):
#load images
imagesAndLabels = imageload.loadImages(imageWidth, imageHeight, imagesPath, labelsPath, True)
images = imagesAndLabels[0]
labels = imagesAndLabels[1]
r = list(range(0, int(percentage*len(images))))
for i in r:
image = images[i]
# function to map image to features
scores = []
for y in self.outputs:
score = self.score(image, y)
scores.append(score)
yPrime = scores.index(max(scores))
y = labels[i]
if yPrime != y:
self.weights[y] = map(operator.add, self.weights[y], image)
self.weights[yPrime] = map(operator.sub, self.weights[yPrime], image)
print "trained {} of data".format(percentage)
def test_one(self, imageWidth, imageHeight, image):
scores = []
imageValues = imageload.imageToValues(image, imageWidth, imageHeight)
for y in self.outputs:
score = self.score(imageValues, y)
scores.append(score)
guess = scores.index(max(scores))
return guess
def test(self, imageWidth, imageHeight, imagesPath, labelsPath):
#load images
imagesAndLabels = imageload.loadImages(imageWidth, imageHeight, imagesPath, labelsPath, False)
images = imagesAndLabels[0]
labels = imagesAndLabels[1]
successes = 0
tests = len(images)
r = list(range(0, len(images)))
for i in r:
image = images[i]
scores = []
for y in self.outputs:
score = self.score(image, y)
scores.append(score)
guess = scores.index(max(scores))
y = labels[i]
if guess == y:
successes = successes + 1
percentageCorrect = ((successes*1.0)/(tests*1.0))*100
#print "{} successes".format(successes)
#print "{} tests".format(tests)
#print "correct {} percent of the time".format(percentageCorrect)
return percentageCorrect