Created
May 10, 2020 12:26
-
-
Save akbardotalam/fc71cd23f2a281f82d2b74af71b44a02 to your computer and use it in GitHub Desktop.
AI assignment
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
import numpy as np | |
import pandas as pd | |
eps = np.finfo(float).eps | |
from numpy import log2, log | |
df = pd.read_csv('data.csv', sep = ',') | |
#this code I used for only values of entropy of target and all features of data. | |
entropy_node = 0 #initilaize entropy | |
values = df.Target.unique() | |
for value in values: | |
fraction = df.Target.value_counts()[value]/len(df.Target) | |
entropy_node += -fraction*np.log2(fraction) | |
def ent(df,attribute): #entropy for each feature in the given data | |
target_variables = df.Target.unique() | |
variables = df[attribute].unique() | |
entropy_attribute = 0 | |
for variable in variables: | |
entropy_each_feature = 0 | |
for target_variable in target_variables: | |
num = len(df[attribute][df[attribute]==variable][df.Target ==target_variable]) #numerator | |
den = len(df[attribute][df[attribute]==variable]) #denominator | |
fraction = num/(den+eps) #pi | |
entropy_each_feature += -fraction*log(fraction+eps) | |
fraction2 = den/len(df) | |
entropy_attribute += -fraction2*entropy_each_feature | |
return(abs(entropy_attribute)) | |
def info_gain(e_dataset, e_attr): | |
return e_dataset - e_attr | |
ig = {i:info_gain(entropy_node, a_entropy[i]) for i in a_entropy} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment