Advertisement
Guest User

Hamoud | DataMining

a guest
Apr 16th, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | None | 0 0
  1. def entropy(*values):
  2.     t=0.0
  3.     for value in values : t+=value
  4.     to_be_returned=0.0
  5.     for value in values :
  6.         if value == 0 : continue
  7.         to_be_returned+= -(value/float(t))*math.log((value/float(t)),2)
  8.     print "Entropy[%s]=%f"%(values,to_be_returned)
  9.     return to_be_returned
  10.  
  11. def IG(*values):
  12.     Parent_Entropy = entropy(*values)
  13.     to_be_returned=Parent_Entropy
  14.     EH=0
  15.     t=0.0
  16.     for value in values : t+=value
  17.     counter = 1
  18.     for value in values :
  19.         message=""
  20.         if counter==1: message = "How many object from the [Class Yes] for 1st child: "
  21.         elif counter==2:message = "How many object from the [Class Yes] for 2nd child: "
  22.         elif counter==3:message = "How many object from the [Class Yes] for 3rd child: "
  23.         else:message = "How many object from the Class Yes for %ith child"%counter
  24.  
  25.         class_one = input(message)
  26.         class_two = value - class_one
  27.         EH+=(value/float(t))*entropy(class_one/float(value),class_two/float(value))
  28.         counter+=1
  29.     print "IG[%s] = %f"%(values,(to_be_returned-EH))
  30.  
  31. -------------------
  32. [~]Author : Hamoud AL-Qusair
  33. [~]Requirement : python interpreter
  34. [~]Usage :
  35.     1) to calculate entropy just call the function `entropy(P,N)` where P,N is number of object from class P,N
  36.         |-> 1.1) it could take as much as possible not only number of P and N it could take up to infinity.
  37.         2) to calculate Information Gain call the function `IG(P,N)` where P,N is number of object from class P,N
  38.         |-> 2.1) it could take as much as possible not only number of P and N it could take up to infinity.
  39.         |-> 2.2) it will ask you to provide how many object from the class 1 for your child
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement