Skip to content

Commit 6368474

Browse files
committed
Fixed a bug in DecisionTreeLearner in count function
1 parent cc9f779 commit 6368474

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

learning.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def plurality_value(examples):
370370

371371
def count(attr, val, examples):
372372
"Count the number of examples that have attr = val."
373-
return len(e[attr] == val for e in examples) #count(e[attr] == val for e in examples)
373+
return sum(e[attr] == val for e in examples)
374374

375375
def all_same_class(examples):
376376
"Are all these examples in the same target class?"

tests/test_learning.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
import math
33
from learning import (
44
parse_csv, weighted_mode, weighted_replicate,
5-
rms_error, manhattan_distance, mean_boolean_error, mean_error
5+
rms_error, manhattan_distance, mean_boolean_error, mean_error,
6+
information_content
67
)
8+
from utils import rounder
79

810

911
def test_parse_csv():
@@ -44,3 +46,14 @@ def test_mean_error():
4446
assert mean_error([1,0], [0,1]) == 1
4547
assert mean_error([0,0], [0,-1]) == 0.5
4648
assert mean_error([0,0.5], [0,-0.5]) == 0.5
49+
50+
def test_information_content():
51+
# Entropy of a 4 sided fair-die
52+
assert information_content([1,1,1,1]) == 2
53+
# Entropy of a fair coin
54+
assert information_content([1,1]) == 1
55+
# Entropy of a biased coin with heads probability 0.99
56+
assert rounder(information_content([0.99,0.01])) == 0.0808
57+
58+
if __name__ == '__main__':
59+
pytest.main()

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