Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added images/decisiontree_fruit.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
267 changes: 159 additions & 108 deletions learning.ipynb

Large diffs are not rendered by default.

29 changes: 28 additions & 1 deletion learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
removeall, unique, product, mode, argmax, argmax_random_tie, isclose, gaussian,
dotproduct, vector_add, scalar_vector_product, weighted_sample_with_replacement,
weighted_sampler, num_or_str, normalize, clip, sigmoid, print_table,
open_data, sigmoid_derivative
open_data, sigmoid_derivative, probability
)

import copy
Expand Down Expand Up @@ -493,6 +493,33 @@ def information_content(values):

# ______________________________________________________________________________


def RandomForest(dataset, n=5):
"""A ensemble of Decision trese trained using bagging and feature bagging."""

predictors = [DecisionTreeLearner(examples=data_bagging(dataset),
attrs=dataset.attrs,
attrnames=dataset.attrnames,
target=dataset.target,
inputs=feature_bagging(datatset)) for _ in range(n)]

def data_bagging(dataset, m=0):
"""Sample m examples with replacement"""
n = len(dataset.examples)
return weighted_sample_with_replacement(m or n, examples, [1]*n)

def feature_bagging(dataset, p=0.7):
"""Feature bagging with probability p to retain an attribute"""
inputs = [i for i in dataset.inputs if probability(p)]
return inputs or dataset.inputs

def predict(example):
return mode(predictor(example) for predictor in predictors)

return predict

# ______________________________________________________________________________

# A decision list is implemented as a list of (test, value) pairs.


Expand Down