Skip to content

Commit b785561

Browse files
Chipe1norvig
authored andcommitted
Learning (aimacode#578)
* Added decisiontreelearner to notebook * Added RandomForest
1 parent be0a10e commit b785561

File tree

3 files changed

+187
-109
lines changed

3 files changed

+187
-109
lines changed

images/decisiontree_fruit.jpg

44.9 KB
Loading

learning.ipynb

Lines changed: 159 additions & 108 deletions
Large diffs are not rendered by default.

learning.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
removeall, unique, product, mode, argmax, argmax_random_tie, isclose, gaussian,
55
dotproduct, vector_add, scalar_vector_product, weighted_sample_with_replacement,
66
weighted_sampler, num_or_str, normalize, clip, sigmoid, print_table,
7-
open_data, sigmoid_derivative
7+
open_data, sigmoid_derivative, probability
88
)
99

1010
import copy
@@ -493,6 +493,33 @@ def information_content(values):
493493

494494
# ______________________________________________________________________________
495495

496+
497+
def RandomForest(dataset, n=5):
498+
"""A ensemble of Decision trese trained using bagging and feature bagging."""
499+
500+
predictors = [DecisionTreeLearner(examples=data_bagging(dataset),
501+
attrs=dataset.attrs,
502+
attrnames=dataset.attrnames,
503+
target=dataset.target,
504+
inputs=feature_bagging(datatset)) for _ in range(n)]
505+
506+
def data_bagging(dataset, m=0):
507+
"""Sample m examples with replacement"""
508+
n = len(dataset.examples)
509+
return weighted_sample_with_replacement(m or n, examples, [1]*n)
510+
511+
def feature_bagging(dataset, p=0.7):
512+
"""Feature bagging with probability p to retain an attribute"""
513+
inputs = [i for i in dataset.inputs if probability(p)]
514+
return inputs or dataset.inputs
515+
516+
def predict(example):
517+
return mode(predictor(example) for predictor in predictors)
518+
519+
return predict
520+
521+
# ______________________________________________________________________________
522+
496523
# A decision list is implemented as a list of (test, value) pairs.
497524

498525

0 commit comments

Comments
 (0)