defent(dataSet):
N=len(dataSet)
n=defaultdict(int)
ent =0for i inrange(N):
n[dataSet[i][-1]]+=1for index, num in n.items():
p = num/N
ent -= p*math.log(p)return ent
defentCondition(dataSet,feature):
n = defaultdict(list)
N =len(dataSet)
entConditon =0for i inrange(N):
n[dataSet[i][feature]].append(dataSet[i])for _, data in n.items():
q =len(data)/N
entConditon+=q*ent(data)return entConditon
dataSet =[[1,1,1,1],[0,0,1,1],[1,1,0,0],[1,1,0,0]]
feature_num=len(dataSet[0])-1
N =len(dataSet)
best_feature=0
best_ent_increase=0
last_ent=ent(dataSet)for i inrange(feature_num):
i_entCondtion = entCondition(dataSet, i)
ent_increase=last_ent-i_entCondtion
print(ent_increase)if ent_increase>best_ent_increase:
best_ent_increase=ent_increase
best_feature=i
print(best_feature)