Guest User

Untitled

a guest
Feb 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. def ent(df,attribute):
  2. target_variables = df.play.unique() #This gives all 'Yes' and 'No'
  3. variables = df[attribute].unique() #This gives different features in that attribute (like 'Sweet')
  4.  
  5.  
  6. entropy_attribute = 0
  7. for variable in variables:
  8. entropy_each_feature = 0
  9. for target_variable in target_variables:
  10. num = len(df[attribute][df[attribute]==variable][df.play ==target_variable]) #numerator
  11. den = len(df[attribute][df[attribute]==variable]) #denominator
  12. fraction = num/(den+eps) #pi
  13. entropy_each_feature += -fraction*log(fraction+eps) #This calculates entropy for one feature like 'Sweet'
  14. fraction2 = den/len(df)
  15. entropy_attribute += -fraction2*entropy_each_feature #Sums up all the entropy ETaste
  16.  
  17. return(abs(entropy_attribute))
Add Comment
Please, Sign In to add comment