Created
July 18, 2019 21:17
-
-
Save acharles7/2bf0da9840130ddb91ce88a016961885 to your computer and use it in GitHub Desktop.
calculate information gain in decision tree algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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