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)"""
5
2
6
3
from utils import *
7
4
import agents , random , operator
@@ -142,7 +139,7 @@ def predict(self, example):
142
139
143
140
#______________________________________________________________________________
144
141
145
- class MajorityLearner (Learner ):
142
+ class PluralityLearner (Learner ):
146
143
"""A very dumb algorithm: always pick the result that was most popular
147
144
in the training data. Makes a baseline for comparison."""
148
145
@@ -278,6 +275,7 @@ def __repr__(self):
278
275
#______________________________________________________________________________
279
276
280
277
class DecisionTreeLearner (Learner ):
278
+ "[Fig. 18.5]"
281
279
282
280
def predict (self , example ):
283
281
if isinstance (self .dt , DecisionTree ):
@@ -290,19 +288,19 @@ def train(self, dataset):
290
288
self .attrnames = dataset .attrnames
291
289
self .dt = self .decision_tree_learning (dataset .examples , dataset .inputs )
292
290
293
- def decision_tree_learning (self , examples , attrs , default = None ):
291
+ def decision_tree_learning (self , examples , attrs , parent_examples = () ):
294
292
if len (examples ) == 0 :
295
- return default
293
+ return self . plurality_value ( parent_examples )
296
294
elif self .all_same_class (examples ):
297
295
return examples [0 ][self .dataset .target ]
298
296
elif len (attrs ) == 0 :
299
- return self .majority_value (examples )
297
+ return self .plurality_value (examples )
300
298
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 )
306
304
tree .add (v , subtree )
307
305
return tree
308
306
@@ -316,7 +314,7 @@ def all_same_class(self, examples):
316
314
class0 = examples [0 ][target ]
317
315
return all (e [target ] == class0 for e in examples )
318
316
319
- def majority_value (self , examples ):
317
+ def plurality_value (self , examples ):
320
318
"""Return the most popular target value for this set of examples.
321
319
(If target is binary, this is the majority; otherwise plurality.)"""
322
320
g = self .dataset .target
@@ -364,7 +362,7 @@ def train(self, dataset):
364
362
self .dl = self .decision_list_learning (Set (dataset .examples ))
365
363
366
364
def decision_list_learning (self , examples ):
367
- """[Fig. 18.14 ]"""
365
+ """[Fig. 18.11 ]"""
368
366
if not examples :
369
367
return [(True , No )]
370
368
t , o , examples_t = self .find_examples (examples )
@@ -501,7 +499,7 @@ def score(learner, size):
501
499
# The Restaurant example from Fig. 18.2
502
500
503
501
def RestaurantDataSet (examples = None ):
504
- "Build a DataSet of Restaurant waiting examples."
502
+ "Build a DataSet of Restaurant waiting examples. [Fig. 18.3] "
505
503
return DataSet (name = 'restaurant' , target = 'Wait' , examples = examples ,
506
504
attrnames = 'Alternate Bar Fri/Sat Hungry Patrons Price '
507
505
+ 'Raining Reservation Type WaitEstimate Wait' )
@@ -573,7 +571,7 @@ def ContinuousXor(n):
573
571
574
572
#______________________________________________________________________________
575
573
576
- def compare (algorithms = [MajorityLearner , NaiveBayesLearner ,
574
+ def compare (algorithms = [PluralityLearner , NaiveBayesLearner ,
577
575
NearestNeighborLearner , DecisionTreeLearner ],
578
576
datasets = [iris , orings , zoo , restaurant , SyntheticRestaurant (20 ),
579
577
Majority (7 , 100 ), Parity (7 , 100 ), Xor (100 )],
0 commit comments