def AdaBoost(L, K):
def train(dataset):<EOL><INDENT>examples, target = dataset.examples, dataset.target<EOL>N = len(examples)<EOL>epsilon = <NUM_LIT:1.>/(<NUM_LIT:2>*N)<EOL>w = [<NUM_LIT:1.>/N] * N<EOL>h, z = [], []<EOL>for k in range(K):<EOL><INDENT>h_k = L(dataset, w)<EOL>h.append(h_k)<EOL>error = sum(weight for example, weight in zip(examples, w)<EOL>if example[target] != h_k(example))<EOL>error = clip(error, epsilon, <NUM_LIT:1>-epsilon)<EOL>for j, example in enumerate(examples):<EOL><INDENT>if example[target] == h_k(example):<EOL><INDENT>w[j] *= error / (<NUM_LIT:1.> - error)<EOL><DEDENT><DEDENT>w = normalize(w)<EOL>z.append(math.log((<NUM_LIT:1.> - error) / error))<EOL><DEDENT>return WeightedMajority(h, z)<EOL><DEDENT>return train<EOL>
[Fig. 18.34]