<s> '''<STR_LIT>''' <EOL> from math import sqrt <EOL> def z_score ( rule ) : <EOL> return sqrt ( rule . coverage ) * ( rule . mean - rule . kb . mean ) / rule . kb . sd <EOL> def t_score ( rule ) : <EOL> return sqrt ( rule . coverage ) * ( rule . mean - rule . kb . mean ) / rule . sd <EOL> def enrichment_score ( rule ) : <EOL> if rule . coverage == rule . kb . n_examples ( ) : <EOL> return <NUM_LIT:1.0> <EOL> if rule . coverage == <NUM_LIT:0> : <EOL> return - <NUM_LIT:1> / float ( rule . kb . n_examples ( ) ) <EOL> increment = { } <EOL> incr1 = <NUM_LIT:1> / float ( rule . coverage ) <EOL> incr2 = <NUM_LIT:1> / float ( rule . kb . n_examples ( ) - rule . coverage ) <EOL> max_diff = <NUM_LIT:0> <EOL> for ex in rule . kb . examples : <EOL> increment [ ex ] = - incr2 <EOL> for ex in rule . examples ( ) : <EOL> increment [ ex ] = incr1 <EOL> partial = <NUM_LIT:0> <EOL> for ex in rule . kb . examples : <EOL> partial += increment [ ex ] <EOL> if partial > max_diff : <EOL> max_diff = partial <EOL> return max_diff <EOL> def wracc ( rule ) : <EOL> nX = rule . coverage <EOL> N = len ( rule . kb . examples ) <EOL> nXY = rule . distribution [ rule . target ] <EOL> nY = rule . kb . distribution [ rule . target ] <EOL> if nX : <EOL> return nX / float ( N ) * ( nXY / float ( nX ) - nY / float ( N ) ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def precision ( rule ) : <EOL> nX = rule . coverage <EOL> nXY = rule . distribution [ rule . target ] <EOL> if nX : <EOL> return nXY / float ( nX ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def chisq ( rule ) : <EOL> N = len ( rule . kb . examples ) <EOL> z = rule . distribution [ rule . target ] / float ( N ) <EOL> x = rule . coverage / float ( N ) <EOL> y = rule . kb . distribution [ rule . target ] / float ( N ) <EOL> if x not in [ <NUM_LIT:0> , <NUM_LIT:1> ] and y not in [ <NUM_LIT:0> , <NUM_LIT:1> ] : <EOL> return N * ( z - x * y ) ** <NUM_LIT:2> / float ( x * y * ( <NUM_LIT:1> - x ) * ( <NUM_LIT:1> - y ) ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def lift ( rule ) : <EOL> nX = float ( rule . coverage ) <EOL> N = float ( len ( rule . kb . examples ) ) <EOL> nXY = rule . distribution [ rule . target ] <EOL> nY = rule . kb . distribution [ rule . target ] <EOL> if nX != <NUM_LIT:0> and N != <NUM_LIT:0> : <EOL> return ( nXY / nX ) / ( nY / N ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def leverage ( rule ) : <EOL> nX = float ( rule . coverage ) <EOL> N = float ( len ( rule . kb . examples ) ) <EOL> nXY = rule . distribution [ rule . target ] <EOL> nY = rule . kb . distribution [ rule . target ] <EOL> if N != <NUM_LIT:0> : <EOL> return nXY / N - ( nX / N ) * ( nY / N ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def kaplan_meier_AUC ( rule ) : <EOL> examples = rule . kb . bits_to_indices ( rule . covered_examples ) <EOL> n_examples = float ( len ( examples ) ) <EOL> if n_examples == <NUM_LIT:0> : <EOL> return <NUM_LIT:0.0> <EOL> def n_alive ( examples , day ) : <EOL> def is_alive ( ex ) : <EOL> return rule . kb . get_score ( ex ) > day <EOL> return len ( filter ( is_alive , examples ) ) <EOL> auc , day = <NUM_LIT:0> , <NUM_LIT:0> <EOL> prev = - <NUM_LIT:1> <EOL> while True : <EOL> alive = n_alive ( examples , day ) <EOL> day += <NUM_LIT> <EOL> curr = alive / n_examples <EOL> if prev != - <NUM_LIT:1> : <EOL> auc += ( prev + curr ) / <NUM_LIT> <EOL> prev = curr <EOL> if alive == <NUM_LIT:0> : <EOL> break <EOL> return auc <EOL> _bounds = { <EOL> z_score : ( <NUM_LIT:0> , float ( '<STR_LIT>' ) ) , <EOL> t_score : ( <NUM_LIT:0> , float ( '<STR_LIT>' ) ) , <EOL> kaplan_meier_AUC : ( <NUM_LIT:0> , float ( '<STR_LIT>' ) ) , <EOL> enrichment_score : ( - float ( '<STR_LIT>' ) , <NUM_LIT:1> ) , <EOL> wracc : ( <NUM_LIT:0> , <NUM_LIT:1> ) , <EOL> precision : ( <NUM_LIT:0> , <NUM_LIT:1> ) , <EOL> chisq : ( <NUM_LIT:0> , float ( '<STR_LIT>' ) ) , <EOL> lift : ( <NUM_LIT:1> , float ( '<STR_LIT>' ) ) , <EOL> leverage : ( <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> } <EOL> def interesting ( rule ) : <EOL> '''<STR_LIT>''' <EOL> score_fun = rule . kb . score_fun <EOL> return _bounds [ score_fun ] [ <NUM_LIT:0> ] < rule . score <= _bounds [ score_fun ] [ <NUM_LIT:1> ] </s>
