Skip to content

Commit cb7b171

Browse files
author
aaron.liu
committed
update
1 parent fd78f0b commit cb7b171

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

ML/LinkedIn Decision Tree.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'''
22
design and implement a decision tree. focus on termination criteria and OOP.
3-
assume boolean features and boolean lable variables.
3+
assume boolean features and boolean label variables.
44
55
'''
66
import math
@@ -80,6 +80,9 @@ def calculate_entropy(instances:List[Instance], feature: Feature) -> float:
8080
# count how many this target feature in instances
8181
if feature in ins.features:
8282
feat_cnt += 1
83+
# # 当p=0或1时 entropy为0
84+
if feat_cnt == 0 or feat_cnt == len(instances):
85+
return 0.0
8386
prob = feat_cnt / len(instances) # feature出现的概率
8487
return - (prob * math.log(prob) + (1 - prob) * math.log(1 - prob))
8588

0 commit comments

Comments
 (0)