<s> import math <EOL> import numpy <EOL> from collections import defaultdict <EOL> class Counter ( defaultdict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , target ) : <EOL> super ( Counter , self ) . __init__ ( int ) <EOL> self . target = target <EOL> self . total = <NUM_LIT:0> <EOL> def add ( self , example ) : <EOL> value = self . target ( example ) <EOL> self [ value ] += <NUM_LIT:1> <EOL> self . total += <NUM_LIT:1> <EOL> class OnlineEntropy ( Counter ) : <EOL> def get_entropy ( self ) : <EOL> s = <NUM_LIT:0.0> <EOL> for count in self . itervalues ( ) : <EOL> p = count / float ( self . total ) <EOL> s += p * math . log ( p , <NUM_LIT:2> ) <EOL> return - s <EOL> class OnlineInformationGain ( object ) : <EOL> def __init__ ( self , attribute , target ) : <EOL> self . attribute = attribute <EOL> self . H = OnlineEntropy ( target ) <EOL> self . G = defaultdict ( lambda : OnlineEntropy ( target ) ) <EOL> def add ( self , example ) : <EOL> self . H . add ( example ) <EOL> value = self . attribute ( example ) <EOL> self . G [ value ] . add ( example ) <EOL> def get_target_class_counts ( self ) : <EOL> return self . H <EOL> def get_branches ( self ) : <EOL> return self . G . items ( ) <EOL> def get_gain ( self ) : <EOL> H1 = self . H . get_entropy ( ) <EOL> H2 = <NUM_LIT:0.0> <EOL> for G in self . G . itervalues ( ) : <EOL> w = G . total / float ( self . H . total ) <EOL> H2 += w * G . get_entropy ( ) <EOL> return H1 - H2 <EOL> class OnlineLogProbability ( object ) : <EOL> def __init__ ( self ) : <EOL> self . d = defaultdict ( int ) <EOL> self . _logtotal = None <EOL> def add ( self , x ) : <EOL> if self . _logtotal is not None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . d [ x ] += <NUM_LIT:1> <EOL> def __getitem__ ( self , x ) : <EOL> if x not in self : <EOL> raise KeyError ( x ) <EOL> if self . _logtotal is None : <EOL> self . _logtotal = numpy . log ( sum ( self . d . itervalues ( ) ) ) <EOL> return numpy . log ( self . d [ x ] ) - self . _logtotal <EOL> def __contains__ ( self , x ) : <EOL> return x in self . d <EOL> def __iter__ ( self ) : <EOL> return iter ( self . d ) <EOL> def __len__ ( self ) : <EOL> return len ( self . d ) <EOL> def iteritems ( self ) : <EOL> for x in self . d : <EOL> yield x , self [ x ] </s>
