<s> from numpy import array , hstack <EOL> from sklearn import metrics , cross_validation , linear_model <EOL> from scipy import sparse <EOL> from itertools import combinations <EOL> import numpy as np <EOL> import pandas as pd <EOL> SEED = <NUM_LIT> <EOL> def group_data ( data , degree = <NUM_LIT:2> , hash = hash ) : <EOL> """<STR_LIT>""" <EOL> new_data = [ ] <EOL> m , n = data . shape <EOL> for indices in combinations ( range ( n ) , degree ) : <EOL> new_data . append ( [ hash ( tuple ( v ) ) for v in data [ : , indices ] ] ) <EOL> return array ( new_data ) . T <EOL> def OneHotEncoder ( data , keymap = None ) : <EOL> if keymap is None : <EOL> keymap = [ ] <EOL> for col in data . T : <EOL> uniques = set ( list ( col ) ) <EOL> keymap . append ( dict ( ( key , i ) for i , key in enumerate ( uniques ) ) ) <EOL> total_pts = data . shape [ <NUM_LIT:0> ] <EOL> outdat = [ ] <EOL> for i , col in enumerate ( data . T ) : <EOL> km = keymap [ i ] <EOL> num_labels = len ( km ) <EOL> spmat = sparse . lil_matrix ( ( total_pts , num_labels ) ) <EOL> for j , val in enumerate ( col ) : <EOL> if val in km : <EOL> spmat [ j , km [ val ] ] = <NUM_LIT:1> <EOL> outdat . append ( spmat ) <EOL> outdat = sparse . hstack ( outdat ) . tocsr ( ) <EOL> return outdat , keymap <EOL> def create_test_submission ( filename , prediction ) : <EOL> content = [ '<STR_LIT>' ] <EOL> for i , p in enumerate ( prediction ) : <EOL> content . append ( '<STR_LIT>' % ( i + <NUM_LIT:1> , p ) ) <EOL> f = open ( filename , '<STR_LIT:w>' ) <EOL> f . write ( '<STR_LIT:\n>' . join ( content ) ) <EOL> f . close ( ) <EOL> print '<STR_LIT>' <EOL> def cv_loop ( X , y , model , N ) : <EOL> mean_auc = <NUM_LIT:0.> <EOL> for i in range ( N ) : <EOL> X_train , X_cv , y_train , y_cv = cross_validation . train_test_split ( <EOL> X , y , test_size = <NUM_LIT> , <EOL> random_state = i * SEED ) <EOL> model . fit ( X_train , y_train ) <EOL> preds = model . predict_proba ( X_cv ) [ : , <NUM_LIT:1> ] <EOL> auc = metrics . auc_score ( y_cv , preds ) <EOL> print "<STR_LIT>" % ( i + <NUM_LIT:1> , N , auc ) <EOL> mean_auc += auc <EOL> return mean_auc / N <EOL> def main ( train = '<STR_LIT>' , test = '<STR_LIT>' , submit = '<STR_LIT>' ) : <EOL> print "<STR_LIT>" <EOL> train_data = pd . read_csv ( train ) <EOL> test_data = pd . read_csv ( test ) <EOL> all_data = np . vstack ( ( train_data . ix [ : , <NUM_LIT:1> : - <NUM_LIT:1> ] , test_data . ix [ : , <NUM_LIT:1> : - <NUM_LIT:1> ] ) ) <EOL> num_train = np . shape ( train_data ) [ <NUM_LIT:0> ] <EOL> print "<STR_LIT>" <EOL> dp = group_data ( all_data , degree = <NUM_LIT:2> ) <EOL> dt = group_data ( all_data , degree = <NUM_LIT:3> ) <EOL> y = array ( train_data . ACTION ) <EOL> X = all_data [ : num_train ] <EOL> X_2 = dp [ : num_train ] <EOL> X_3 = dt [ : num_train ] <EOL> X_test = all_data [ num_train : ] <EOL> X_test_2 = dp [ num_train : ] <EOL> X_test_3 = dt [ num_train : ] <EOL> X_train_all = np . hstack ( ( X , X_2 , X_3 ) ) <EOL> X_test_all = np . hstack ( ( X_test , X_test_2 , X_test_3 ) ) <EOL> num_features = X_train_all . shape [ <NUM_LIT:1> ] <EOL> model = linear_model . LogisticRegression ( ) <EOL> Xts = [ OneHotEncoder ( X_train_all [ : , [ i ] ] ) [ <NUM_LIT:0> ] for i in range ( num_features ) ] <EOL> print "<STR_LIT>" <EOL> score_hist = [ ] <EOL> N = <NUM_LIT:10> <EOL> good_features = set ( [ ] ) <EOL> while len ( score_hist ) < <NUM_LIT:2> or score_hist [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] > score_hist [ - <NUM_LIT:2> ] [ <NUM_LIT:0> ] : <EOL> scores = [ ] <EOL> for f in range ( len ( Xts ) ) : <EOL> if f not in good_features : <EOL> feats = list ( good_features ) + [ f ] <EOL> Xt = sparse . hstack ( [ Xts [ j ] for j in feats ] ) . tocsr ( ) <EOL> score = cv_loop ( Xt , y , model , N ) <EOL> scores . append ( ( score , f ) ) <EOL> print "<STR_LIT>" % ( f , score ) <EOL> good_features . add ( sorted ( scores ) [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> score_hist . append ( sorted ( scores ) [ - <NUM_LIT:1> ] ) <EOL> print "<STR_LIT>" % sorted ( list ( good_features ) ) <EOL> good_features . remove ( score_hist [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> good_features = sorted ( list ( good_features ) ) <EOL> print "<STR_LIT>" % good_features <EOL> print "<STR_LIT>" <EOL> score_hist = [ ] <EOL> Xt = sparse . hstack ( [ Xts [ j ] for j in good_features ] ) . tocsr ( ) <EOL> Cvals = np . logspace ( - <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:15> , base = <NUM_LIT:2> ) <EOL> for C in Cvals : <EOL> model . C = C <EOL> score = cv_loop ( Xt , y , model , N ) <EOL> score_hist . append ( ( score , C ) ) <EOL> print "<STR_LIT>" % ( C , score ) <EOL> bestC = sorted ( score_hist ) [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> print "<STR_LIT>" % ( bestC ) <EOL> print "<STR_LIT>" <EOL> Xt = np . vstack ( ( X_train_all [ : , good_features ] , X_test_all [ : , good_features ] ) ) <EOL> Xt , keymap = OneHotEncoder ( Xt ) <EOL> X_train = Xt [ : num_train ] <EOL> X_test = Xt [ num_train : ] <EOL> print "<STR_LIT>" <EOL> model . fit ( X_train , y ) <EOL> print "<STR_LIT>" <EOL> preds = model . predict_proba ( X_test ) [ : , <NUM_LIT:1> ] <EOL> create_test_submission ( submit , preds ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> args = { '<STR_LIT:train>' : '<STR_LIT>' , <EOL> '<STR_LIT:test>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> main ( ** args ) </s>
