Skip to content

Commit 426b06f

Browse files
committed
Break information-gain ties randomly too.
1 parent be60670 commit 426b06f

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

learning.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class NaiveBayesLearner(Learner):
166166

167167
def train(self, dataset):
168168
"""Just count the target/attr/val occurrences.
169-
Count how many times each value of each attribute occurs.
169+
Count how many times each value of each input attribute occurs.
170170
Store count in N[targetvalue][attr][val]. Let
171171
N[targetvalue][attr][None] be the sum over all vals."""
172172
self.dataset = dataset
@@ -331,7 +331,8 @@ def all_same_class(self, examples):
331331

332332
def choose_attribute(self, attrs, examples):
333333
"Choose the attribute with the highest information gain."
334-
return argmax(attrs, lambda a: self.information_gain(a, examples))
334+
return argmax_random_tie(attrs,
335+
lambda a: self.information_gain(a, examples))
335336

336337
def information_gain(self, attr, examples):
337338
def I(examples):
@@ -539,8 +540,9 @@ def T(attrname, branches):
539540
__doc__ += """
540541
[Fig. 18.6]
541542
>>> restaurant_learner = DecisionTreeLearner()
543+
>>> random.seed(437)
542544
>>> restaurant_learner.train(restaurant)
543-
>>> restaurant_learner.dt.display() #doctest:+ELLIPSIS
545+
>>> restaurant_learner.dt.display()
544546
Test Patrons
545547
Patrons = None ==> RESULT = No
546548
Patrons = Full ==> Test Hungry
@@ -549,7 +551,7 @@ def T(attrname, branches):
549551
Type = Thai ==> Test Fri/Sat
550552
Fri/Sat = Yes ==> RESULT = Yes
551553
Fri/Sat = No ==> RESULT = No
552-
Type = French ==> RESULT = ...
554+
Type = French ==> RESULT = Yes
553555
Type = Italian ==> RESULT = No
554556
Hungry = No ==> RESULT = No
555557
Patrons = Some ==> RESULT = Yes

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy