Skip to content

Instantly share code, notes, and snippets.

@acharles7
Created July 18, 2019 21:17
Show Gist options
  • Save acharles7/2bf0da9840130ddb91ce88a016961885 to your computer and use it in GitHub Desktop.
Save acharles7/2bf0da9840130ddb91ce88a016961885 to your computer and use it in GitHub Desktop.
calculate information gain in decision tree algorithm
# x = np.random.rand(10,2) * 10
# y = np.random.rand(10,1) * 10
def calculate_entropy(pi):
total = 0
for p in pi:
p = p / sum(pi)
if p != 0:
total += p * np.log2(p)
else:
total += 0
total *= -1
return total
def information_gain(x, y):
total = 0
for v in x:
total += sum(v) / sum(y) * calculate_entropy(v)
gain = calculate_entropy(y) - total
return gain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment