Skip to content

Commit 40c8750

Browse files
Converted learning.py to 3rd edition.
1 parent 4c430e1 commit 40c8750

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

learning.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
"""Learn to estimate functions from examples. (Chapters 18-20)"""
2-
3-
# (Written for the second edition of AIMA; expect some discrepanciecs
4-
# from the third edition until this gets reviewed.)
1+
"""Learn to estimate functions from examples. (Chapters 18-20)"""
52

63
from utils import *
74
import agents, random, operator
@@ -142,7 +139,7 @@ def predict(self, example):
142139

143140
#______________________________________________________________________________
144141

145-
class MajorityLearner(Learner):
142+
class PluralityLearner(Learner):
146143
"""A very dumb algorithm: always pick the result that was most popular
147144
in the training data. Makes a baseline for comparison."""
148145

@@ -278,6 +275,7 @@ def __repr__(self):
278275
#______________________________________________________________________________
279276

280277
class DecisionTreeLearner(Learner):
278+
"[Fig. 18.5]"
281279

282280
def predict(self, example):
283281
if isinstance(self.dt, DecisionTree):
@@ -290,19 +288,19 @@ def train(self, dataset):
290288
self.attrnames = dataset.attrnames
291289
self.dt = self.decision_tree_learning(dataset.examples, dataset.inputs)
292290

293-
def decision_tree_learning(self, examples, attrs, default=None):
291+
def decision_tree_learning(self, examples, attrs, parent_examples=()):
294292
if len(examples) == 0:
295-
return default
293+
return self.plurality_value(parent_examples)
296294
elif self.all_same_class(examples):
297295
return examples[0][self.dataset.target]
298296
elif len(attrs) == 0:
299-
return self.majority_value(examples)
297+
return self.plurality_value(examples)
300298
else:
301-
best = self.choose_attribute(attrs, examples)
302-
tree = DecisionTree(best, self.attrnames[best])
303-
for (v, examples_i) in self.split_by(best, examples):
304-
subtree = self.decision_tree_learning(examples_i,
305-
removeall(best, attrs), self.majority_value(examples))
299+
A = self.choose_attribute(attrs, examples)
300+
tree = DecisionTree(A, self.attrnames[A])
301+
for (v, examples_i) in self.split_by(A, examples):
302+
subtree = self.decision_tree_learning(
303+
examples_i, removeall(A, attrs), examples)
306304
tree.add(v, subtree)
307305
return tree
308306

@@ -316,7 +314,7 @@ def all_same_class(self, examples):
316314
class0 = examples[0][target]
317315
return all(e[target] == class0 for e in examples)
318316

319-
def majority_value(self, examples):
317+
def plurality_value(self, examples):
320318
"""Return the most popular target value for this set of examples.
321319
(If target is binary, this is the majority; otherwise plurality.)"""
322320
g = self.dataset.target
@@ -364,7 +362,7 @@ def train(self, dataset):
364362
self.dl = self.decision_list_learning(Set(dataset.examples))
365363

366364
def decision_list_learning(self, examples):
367-
"""[Fig. 18.14]"""
365+
"""[Fig. 18.11]"""
368366
if not examples:
369367
return [(True, No)]
370368
t, o, examples_t = self.find_examples(examples)
@@ -501,7 +499,7 @@ def score(learner, size):
501499
# The Restaurant example from Fig. 18.2
502500

503501
def RestaurantDataSet(examples=None):
504-
"Build a DataSet of Restaurant waiting examples."
502+
"Build a DataSet of Restaurant waiting examples. [Fig. 18.3]"
505503
return DataSet(name='restaurant', target='Wait', examples=examples,
506504
attrnames='Alternate Bar Fri/Sat Hungry Patrons Price '
507505
+ 'Raining Reservation Type WaitEstimate Wait')
@@ -573,7 +571,7 @@ def ContinuousXor(n):
573571

574572
#______________________________________________________________________________
575573

576-
def compare(algorithms=[MajorityLearner, NaiveBayesLearner,
574+
def compare(algorithms=[PluralityLearner, NaiveBayesLearner,
577575
NearestNeighborLearner, DecisionTreeLearner],
578576
datasets=[iris, orings, zoo, restaurant, SyntheticRestaurant(20),
579577
Majority(7, 100), Parity(7, 100), Xor(100)],

0 commit comments

Comments
 (0)