-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecision_stump.py
87 lines (77 loc) · 1.92 KB
/
decision_stump.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
76
77
78
79
80
81
82
83
84
85
86
87
from random import uniform,random
def sign(x):
if x > 0:
return 1
else:
return -1
def generate_dataset():
result = []
for i in range(20):
x = uniform(-1,1)
if random() > 0.2:
y = sign(x)
else:
y = -sign(x)
#print x,y
result.append((x,y))
return result
def calculate(dataset,s,theta):
Ein = 0.0
for sample in dataset:
if s*sign(sample[0]-theta) != sample[1]:
Ein += 1.0
return Ein/len(dataset)
def decision_stump(dataset,s):
ping = len(dataset)
theta_result = 0
for i in range(len(dataset)-2):
theta = (dataset[i][0]+dataset[i+1][0])/2.0
Ein = calculate(dataset,s,theta)
if Ein < ping:
ping = Ein
theta_result = theta
return (ping,theta_result)
def parse_multi_dimen():
fp = open("decision_stump_train.txt",'r')
train_data = []
for line in fp.readlines():
train_data.append((line.strip().split()))
fp.close()
return train_data
def gen_dimen_dataset(train_data,i):
a = [(float(t[i]),int(t[-1])) for t in train_data]
return a
def test(dataset):
sorted_dataset = sorted(dataset,key=lambda set: set[0])
(Ein_pos,theta_pos) = decision_stump(sorted_dataset,1)
(Ein_neg,theta_neg) = decision_stump(sorted_dataset,-1)
if Ein_pos < Ein_neg:
Ein_result = Ein_pos
theta_result = theta_pos
s_result = 1
else:
Ein_result = Ein_neg
theta_result = theta_neg
s_result = -1
return (theta_result,s_result,Ein_result)
if __name__ == "__main__":
EIN = 0.0
EOUT = 0.0
for i in range(5000):
dataset = generate_dataset()
sorted_dataset = sorted(dataset,key=lambda set: set[0])
(Ein_pos,theta_pos) = decision_stump(sorted_dataset,1)
(Ein_neg,theta_neg) = decision_stump(sorted_dataset,-1)
if Ein_pos < Ein_neg:
Ein_result = Ein_pos
theta_result = theta_pos
s_result = 1
else:
Ein_result = Ein_neg
theta_result = theta_neg
s_result = -1
Eout_result = 0.5 + 0.3 * s_result* (abs(theta_result)-1)
EIN += Ein_result
EOUT += Eout_result
print EIN/5000
print EOUT/5000