-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboosting-test.py
executable file
·63 lines (40 loc) · 1.62 KB
/
boosting-test.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
import random
import boosting
from utils import sign
def simpleTest():
def target(x):
if x[2] > 0.5 or x[3] > 0.5:
return 1 if random.random() > 0.05 else -1
return -1
examples = [[random.random() for _ in range(10)] for _ in range(1000)]
labels = [target(x) for x in examples]
trainingData = list(zip(examples, labels))
testData = [[random.random() for _ in range(10)] for _ in range(1000)]
testLabels = [target(x) for x in testData]
def testCoordinate(samples, j):
values = [sign(x[j] - 0.5) * y for (x,y) in samples]
return len([z for z in values if z > 0]) / len(values)
def bestCoordinate(samples, n):
return max(range(n), key=lambda j: testCoordinate(samples, j))
# find the single coordinate and a threshold value that works best
def singleCoordinateLearner(drawExample):
samples = [drawExample() for _ in range(100)]
n = len(samples[0][0])
j = bestCoordinate(samples, n)
return lambda x: x[j] > 0.5
finalH, finalDistr = boosting.boost(trainingData, singleCoordinateLearner, 100)
finalError = len([x for x in testData if finalH(x) != target(x)]) / len(testData)
print(finalError)
def error(h, data):
return sum(1 for x,y in data if h(x) != y) / len(data)
def runAdult():
from data import adult
from decisionstump import buildDecisionStump
train, test = adult.load()
weakLearner = buildDecisionStump
rounds = 20
h = boosting.boost(train, weakLearner, rounds)
print("Training error: %G" % error(h, train))
print("Test error: %G" % error(h, test))
if __name__ == "__main__":
runAdult()