I can't seem to figure out why there is an error. I am learning decision tree algorithm.
def gini(rows):
"""calculate the gini impurity for a list of rows."""
counts = class_counts(rows)
impurity = 1
for lbl in counts:
prob_of_lbl = counts[lbl] / float(len(rows))
impurity -= prob_of_lbl**2
return impurity
def info_gain(left, right, current_uncertainty):
"""Information Gain"""
#The uncertainty of the starting node, minus the weighted
#impurity of two child nodes.
p = float(len(left)) / (len(left) + len(right))
return current_uncertainty - p * gini(left) - (1 - p) * gini(right)