<s> '''<STR_LIT>''' <EOL> from datetime import datetime <EOL> from csv import DictReader <EOL> from math import exp , log , sqrt <EOL> import marshal <EOL> train = '<STR_LIT>' <EOL> test = '<STR_LIT>' <EOL> submission = '<STR_LIT>' <EOL> alpha = <NUM_LIT> <EOL> beta = <NUM_LIT:1.> <EOL> L1 = <NUM_LIT> <EOL> L2 = <NUM_LIT> <EOL> D = <NUM_LIT:2> ** <NUM_LIT> <EOL> interaction = True <EOL> SUB = True <EOL> epoch = <NUM_LIT:3> <EOL> holdafter = <NUM_LIT:30> if SUB else <NUM_LIT> <EOL> holdout = None <EOL> inter_s = "<STR_LIT>" <EOL> inter = [ ] <EOL> inter_s2 = "<STR_LIT>" <EOL> inter2 = [ ] <EOL> featdict = { "<STR_LIT:a>" : "<STR_LIT>" , "<STR_LIT:b>" : "<STR_LIT>" , "<STR_LIT:c>" : "<STR_LIT>" , "<STR_LIT:d>" : "<STR_LIT>" , "<STR_LIT:e>" : "<STR_LIT>" , "<STR_LIT:f>" : "<STR_LIT>" , "<STR_LIT:g>" : "<STR_LIT>" , "<STR_LIT:h>" : "<STR_LIT>" , "<STR_LIT:i>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:k>" : "<STR_LIT>" , "<STR_LIT:l>" : "<STR_LIT>" , "<STR_LIT:m>" : "<STR_LIT>" , "<STR_LIT:n>" : "<STR_LIT>" , "<STR_LIT:o>" : "<STR_LIT>" , "<STR_LIT:p>" : "<STR_LIT>" , "<STR_LIT:q>" : "<STR_LIT>" , "<STR_LIT:r>" : "<STR_LIT>" , "<STR_LIT:s>" : "<STR_LIT>" , "<STR_LIT:t>" : "<STR_LIT>" , "<STR_LIT:u>" : "<STR_LIT>" , "<STR_LIT:w>" : "<STR_LIT>" , "<STR_LIT:x>" : "<STR_LIT>" , "<STR_LIT:y>" : "<STR_LIT>" , "<STR_LIT:z>" : "<STR_LIT>" , "<STR_LIT:1>" : "<STR_LIT>" , "<STR_LIT:2>" : "<STR_LIT>" , "<STR_LIT:3>" : "<STR_LIT>" , "<STR_LIT:4>" : "<STR_LIT>" } <EOL> for i in inter_s . split ( "<STR_LIT:U+002C>" ) : <EOL> inter . append ( ( featdict [ i [ <NUM_LIT:0> ] ] , featdict [ i [ <NUM_LIT:1> ] ] ) ) <EOL> for i in inter_s2 . split ( "<STR_LIT:U+002C>" ) : <EOL> inter2 . append ( ( featdict [ i [ <NUM_LIT:0> ] ] , featdict [ i [ <NUM_LIT:1> ] ] ) ) <EOL> for i in xrange ( <NUM_LIT> , <NUM_LIT> ) : <EOL> co = "<STR_LIT:C>" + str ( i ) <EOL> inter . append ( ( co , "<STR_LIT>" ) ) <EOL> inter2 . append ( ( co , "<STR_LIT>" ) ) <EOL> ipcate = marshal . load ( open ( "<STR_LIT>" ) ) <EOL> def convt ( s , t ) : <EOL> s = s . split ( "<STR_LIT:_>" ) [ <NUM_LIT:1> ] <EOL> if int ( s ) <= <NUM_LIT> : <EOL> return t + "<STR_LIT:_>" + s <EOL> else : <EOL> return t + "<STR_LIT:_>" + "<STR_LIT:l>" <EOL> class ftrl_proximal ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , alpha , beta , L1 , L2 , D , interaction ) : <EOL> self . alpha = alpha <EOL> self . beta = beta <EOL> self . L1 = L1 <EOL> self . L2 = L2 <EOL> self . D = D <EOL> self . interaction = interaction <EOL> self . n = [ <NUM_LIT:0.> ] * D <EOL> self . z = [ <NUM_LIT:0.> ] * D <EOL> self . w = [ <NUM_LIT:0.> ] * D <EOL> def _indices ( self , x ) : <EOL> '''<STR_LIT>''' <EOL> yield <NUM_LIT:0> <EOL> D = self . D <EOL> for feat in x : <EOL> index = abs ( hash ( feat ) ) % D <EOL> yield index <EOL> def predict ( self , x ) : <EOL> '''<STR_LIT>''' <EOL> w = self . w <EOL> wTx = <NUM_LIT:0.> <EOL> for i in self . _indices ( x ) : <EOL> wTx += w [ i ] <EOL> return <NUM_LIT:1.> / ( <NUM_LIT:1.> + exp ( - max ( min ( wTx , <NUM_LIT> ) , - <NUM_LIT> ) ) ) <EOL> def update ( self , x , p , y ) : <EOL> '''<STR_LIT>''' <EOL> alpha = self . alpha <EOL> n = self . n <EOL> z = self . z <EOL> w = self . w <EOL> g = p - y <EOL> tmp = <NUM_LIT:0> <EOL> for i in self . _indices ( x ) : <EOL> sigma = ( sqrt ( n [ i ] + g * g ) - sqrt ( n [ i ] ) ) / alpha <EOL> z [ i ] += g - sigma * w [ i ] <EOL> n [ i ] += g * g <EOL> sign = - <NUM_LIT:1.> if z [ i ] < <NUM_LIT:0> else <NUM_LIT:1.> <EOL> if sign * z [ i ] <= L1 : <EOL> w [ i ] = <NUM_LIT:0.> <EOL> else : <EOL> w [ i ] = ( sign * L1 - z [ i ] ) / ( ( beta + sqrt ( n [ i ] ) ) / alpha + L2 ) <EOL> def logloss ( p , y ) : <EOL> '''<STR_LIT>''' <EOL> p = max ( min ( p , <NUM_LIT:1.> - <NUM_LIT> ) , <NUM_LIT> ) <EOL> return - log ( p ) if y == <NUM_LIT:1.> else - log ( <NUM_LIT:1.> - p ) <EOL> def data ( path , D ) : <EOL> '''<STR_LIT>''' <EOL> for t , row in enumerate ( DictReader ( open ( path ) ) ) : <EOL> ID = row [ '<STR_LIT:id>' ] <EOL> del row [ '<STR_LIT:id>' ] <EOL> y = <NUM_LIT:0.> <EOL> if '<STR_LIT>' in row : <EOL> if row [ '<STR_LIT>' ] == '<STR_LIT:1>' : <EOL> y = <NUM_LIT:1.> <EOL> del row [ '<STR_LIT>' ] <EOL> date = int ( row [ '<STR_LIT>' ] [ <NUM_LIT:4> : <NUM_LIT:6> ] ) <EOL> row [ "<STR_LIT>" ] = convt ( row [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> del row [ "<STR_LIT>" ] <EOL> row [ "<STR_LIT>" ] = row [ "<STR_LIT>" ] <EOL> if row [ "<STR_LIT>" ] [ - <NUM_LIT:3> : ] == "<STR_LIT>" : <EOL> row [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> row [ "<STR_LIT>" ] = row [ "<STR_LIT>" ] [ : - <NUM_LIT:3> ] <EOL> row [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> if row [ "<STR_LIT>" ] [ <NUM_LIT:2> : ] in ipcate : <EOL> row [ "<STR_LIT>" ] = "<STR_LIT>" + str ( ipcate [ row [ "<STR_LIT>" ] [ <NUM_LIT:2> : ] ] ) <EOL> isApp = row [ "<STR_LIT>" ] == "<STR_LIT>" <EOL> if isApp : <EOL> del row [ "<STR_LIT>" ] <EOL> del row [ "<STR_LIT>" ] <EOL> del row [ "<STR_LIT>" ] <EOL> else : <EOL> del row [ "<STR_LIT>" ] <EOL> del row [ "<STR_LIT>" ] <EOL> del row [ "<STR_LIT>" ] <EOL> x = [ ] <EOL> if interaction : <EOL> if not isApp : <EOL> for pair in inter : <EOL> x . append ( row [ pair [ <NUM_LIT:0> ] ] + "<STR_LIT:_>" + row [ pair [ <NUM_LIT:1> ] ] ) <EOL> else : <EOL> for pair in inter2 : <EOL> x . append ( row [ pair [ <NUM_LIT:0> ] ] + "<STR_LIT:_>" + row [ pair [ <NUM_LIT:1> ] ] ) <EOL> del row [ "<STR_LIT>" ] <EOL> for key in row : <EOL> value = row [ key ] <EOL> x . append ( value ) <EOL> yield t , date , ID , x , y , isApp <EOL> start = datetime . now ( ) <EOL> learner1 = ftrl_proximal ( alpha , beta , L1 , L2 , D , interaction ) <EOL> learner2 = ftrl_proximal ( alpha , beta , L1 , L2 , D , interaction ) <EOL> for e in xrange ( epoch ) : <EOL> loss1 = <NUM_LIT:0.> <EOL> loss2 = <NUM_LIT:0.> <EOL> count1 = <NUM_LIT:1> <EOL> count2 = <NUM_LIT:1> <EOL> loss = <NUM_LIT:0.> <EOL> count = <NUM_LIT:1> <EOL> localcount = <NUM_LIT:0> <EOL> learner1 . alpha = <NUM_LIT> - <NUM_LIT> * e <EOL> learner2 . alpha = <NUM_LIT> - <NUM_LIT> * e <EOL> for t , date , ID , x , y , isApp in data ( train , D ) : <EOL> if not isApp : <EOL> p = learner1 . predict ( x ) <EOL> else : <EOL> p = learner2 . predict ( x ) <EOL> localcount += <NUM_LIT:1> <EOL> if localcount % <NUM_LIT> == <NUM_LIT:0> : <EOL> if ( holdafter and date > holdafter ) or ( holdout and t % holdout == <NUM_LIT:0> ) : <EOL> print "<STR_LIT>" + str ( localcount ) <EOL> else : <EOL> print "<STR_LIT>" + str ( localcount ) <EOL> if ( holdafter and date > holdafter ) or ( holdout and t % holdout == <NUM_LIT:0> ) : <EOL> loss += logloss ( p , y ) <EOL> count += <NUM_LIT:1> <EOL> if isApp : <EOL> loss1 += logloss ( p , y ) <EOL> count1 += <NUM_LIT:1> <EOL> else : <EOL> loss2 += logloss ( p , y ) <EOL> count2 += <NUM_LIT:1> <EOL> else : <EOL> if not isApp : <EOL> learner1 . update ( x , p , y ) <EOL> else : <EOL> learner2 . update ( x , p , y ) <EOL> if not SUB : <EOL> print ( '<STR_LIT>' % ( e , loss / count , loss1 / count1 , loss2 / count2 , str ( datetime . now ( ) - start ) ) ) <EOL> def sub ( ) : <EOL> with open ( submission , '<STR_LIT:w>' ) as outfile : <EOL> for t , date , ID , x , y , isApp in data ( test , D ) : <EOL> if not isApp : <EOL> p = learner1 . predict ( x ) <EOL> else : <EOL> p = learner2 . predict ( x ) <EOL> outfile . write ( '<STR_LIT>' % p ) <EOL> sub ( ) </s>
