<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import numpy as np <EOL> from sklearn import preprocessing <EOL> from sklearn . linear_model import LogisticRegression <EOL> from sklearn . metrics import roc_auc_score <EOL> import XGBoostClassifier as xg <EOL> from sklearn . cross_validation import StratifiedKFold <EOL> SEED = <NUM_LIT> <EOL> def load_data ( filename , use_labels = True ) : <EOL> """<STR_LIT>""" <EOL> data = np . loadtxt ( open ( filename ) , delimiter = '<STR_LIT:U+002C>' , <EOL> usecols = range ( <NUM_LIT:1> , <NUM_LIT:9> ) , skiprows = <NUM_LIT:1> ) <EOL> if use_labels : <EOL> labels = np . loadtxt ( open ( filename ) , delimiter = '<STR_LIT:U+002C>' , <EOL> usecols = [ <NUM_LIT:0> ] , skiprows = <NUM_LIT:1> ) <EOL> else : <EOL> labels = np . zeros ( data . shape [ <NUM_LIT:0> ] ) <EOL> return labels , data <EOL> def save_results ( predictions , filename ) : <EOL> """<STR_LIT>""" <EOL> with open ( filename , '<STR_LIT:w>' ) as f : <EOL> f . write ( "<STR_LIT>" ) <EOL> for i , pred in enumerate ( predictions ) : <EOL> f . write ( "<STR_LIT>" % ( i + <NUM_LIT:1> , pred ) ) <EOL> def bagged_set ( X_t , y_c , model , seed , estimators , xt , update_seed = True ) : <EOL> baggedpred = [ <NUM_LIT:0.0> for d in range ( <NUM_LIT:0> , ( xt . shape [ <NUM_LIT:0> ] ) ) ] <EOL> for n in range ( <NUM_LIT:0> , estimators ) : <EOL> if update_seed : <EOL> model . set_params ( random_state = seed + n ) <EOL> model . fit ( X_t , y_c ) <EOL> preds = model . predict_proba ( xt ) [ : , <NUM_LIT:1> ] <EOL> for j in range ( <NUM_LIT:0> , ( xt . shape [ <NUM_LIT:0> ] ) ) : <EOL> baggedpred [ j ] += preds [ j ] <EOL> for j in range ( <NUM_LIT:0> , len ( baggedpred ) ) : <EOL> baggedpred [ j ] /= float ( estimators ) <EOL> return np . array ( baggedpred ) <EOL> def printfilcsve ( X , filename ) : <EOL> np . savetxt ( filename , X ) <EOL> def Make_3way ( X , Xt , model , y , seed , n , atleast ) : <EOL> grand_auc = <NUM_LIT:0.0> <EOL> kfolder = StratifiedKFold ( y , n_folds = n , shuffle = True , random_state = seed ) <EOL> encoder = preprocessing . OneHotEncoder ( ) <EOL> Xs = encoder . fit_transform ( X ) <EOL> i = <NUM_LIT:0> <EOL> for train_index , test_index in kfolder : <EOL> X_train , X_cv = Xs [ train_index ] , Xs [ test_index ] <EOL> y_train , y_cv = np . array ( y ) [ train_index ] , np . array ( y ) [ test_index ] <EOL> preds = bagged_set ( X_train , y_train , model , SEED , <NUM_LIT:1> , X_cv , update_seed = True ) <EOL> roc_auc = roc_auc_score ( y_cv , preds ) <EOL> print "<STR_LIT>" % ( i + <NUM_LIT:1> , n , roc_auc ) <EOL> grand_auc += roc_auc <EOL> grand_auc /= n <EOL> print ( "<STR_LIT>" % ( grand_auc ) ) <EOL> columns_length = X . shape [ <NUM_LIT:1> ] <EOL> for j in range ( columns_length ) : <EOL> for d in range ( j + <NUM_LIT:1> , columns_length ) : <EOL> print ( "<STR_LIT>" % ( j , d ) ) <EOL> new_column_train = X [ : , j ] + X [ : , d ] <EOL> new_column_test = Xt [ : , j ] + Xt [ : , d ] <EOL> X = np . column_stack ( ( X , new_column_train ) ) <EOL> Xt = np . column_stack ( ( Xt , new_column_test ) ) <EOL> encoder = preprocessing . OneHotEncoder ( ) <EOL> Xs = encoder . fit_transform ( X ) <EOL> mean_auc = <NUM_LIT:0.0> <EOL> i = <NUM_LIT:0> <EOL> for train_index , test_index in kfolder : <EOL> X_train , X_cv = Xs [ train_index ] , Xs [ test_index ] <EOL> y_train , y_cv = np . array ( y ) [ train_index ] , np . array ( y ) [ test_index ] <EOL> preds = bagged_set ( X_train , y_train , model , SEED , <NUM_LIT:1> , X_cv , update_seed = True ) <EOL> roc_auc = roc_auc_score ( y_cv , preds ) <EOL> print "<STR_LIT>" % ( i + <NUM_LIT:1> , n , roc_auc ) <EOL> mean_auc += roc_auc <EOL> mean_auc /= n <EOL> if mean_auc > grand_auc + atleast : <EOL> print ( "<STR_LIT>" % ( j , d , mean_auc , grand_auc , mean_auc - grand_auc ) ) <EOL> grand_auc = mean_auc <EOL> else : <EOL> X = np . delete ( X , - <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> Xt = np . delete ( Xt , - <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> for j in range ( columns_length ) : <EOL> for d in range ( j + <NUM_LIT:1> , columns_length ) : <EOL> for m in range ( d + <NUM_LIT:1> , columns_length ) : <EOL> print ( "<STR_LIT>" % ( j , d , m ) ) <EOL> new_column_train = X [ : , j ] + X [ : , d ] + X [ : , m ] <EOL> new_column_test = Xt [ : , j ] + Xt [ : , d ] + Xt [ : , m ] <EOL> X = np . column_stack ( ( X , new_column_train ) ) <EOL> Xt = np . column_stack ( ( Xt , new_column_test ) ) <EOL> encoder = preprocessing . OneHotEncoder ( ) <EOL> Xs = encoder . fit_transform ( X ) <EOL> mean_auc = <NUM_LIT:0.0> <EOL> i = <NUM_LIT:0> <EOL> for train_index , test_index in kfolder : <EOL> X_train , X_cv = Xs [ train_index ] , Xs [ test_index ] <EOL> y_train , y_cv = np . array ( y ) [ train_index ] , np . array ( y ) [ test_index ] <EOL> preds = bagged_set ( X_train , y_train , model , SEED , <NUM_LIT:1> , X_cv , update_seed = True ) <EOL> roc_auc = roc_auc_score ( y_cv , preds ) <EOL> print "<STR_LIT>" % ( i + <NUM_LIT:1> , n , roc_auc ) <EOL> mean_auc += roc_auc <EOL> mean_auc /= n <EOL> if mean_auc > grand_auc + atleast : <EOL> print ( "<STR_LIT>" % ( j , d , m , mean_auc , grand_auc , mean_auc - grand_auc ) ) <EOL> grand_auc = mean_auc <EOL> else : <EOL> X = np . delete ( X , - <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> Xt = np . delete ( Xt , - <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> return X , Xt <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> filename = "<STR_LIT>" <EOL> model = LogisticRegression ( C = <NUM_LIT> , penalty = "<STR_LIT>" ) <EOL> print "<STR_LIT>" <EOL> y , X = load_data ( '<STR_LIT>' ) <EOL> y_test , X_test = load_data ( '<STR_LIT>' , use_labels = False ) <EOL> X , X_test = Make_3way ( X , X_test , model , y , <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT> ) <EOL> encoder = preprocessing . OneHotEncoder ( ) <EOL> encoder . fit ( np . vstack ( ( X , X_test ) ) ) <EOL> X = encoder . transform ( X ) <EOL> X_test = encoder . transform ( X_test ) <EOL> train_stacker = [ <NUM_LIT:0.0> for k in range ( <NUM_LIT:0> , ( X . shape [ <NUM_LIT:0> ] ) ) ] <EOL> mean_auc = <NUM_LIT:0.0> <EOL> bagging = <NUM_LIT:1> <EOL> n = <NUM_LIT:5> <EOL> kfolder = StratifiedKFold ( y , n_folds = n , shuffle = True , random_state = SEED ) <EOL> i = <NUM_LIT:0> <EOL> for train_index , test_index in kfolder : <EOL> X_train , X_cv = X [ train_index ] , X [ test_index ] <EOL> y_train , y_cv = np . array ( y ) [ train_index ] , np . array ( y ) [ test_index ] <EOL> preds = bagged_set ( X_train , y_train , model , SEED , bagging , X_cv , update_seed = True ) <EOL> roc_auc = roc_auc_score ( y_cv , preds ) <EOL> print "<STR_LIT>" % ( i + <NUM_LIT:1> , n , roc_auc ) <EOL> mean_auc += roc_auc <EOL> no = <NUM_LIT:0> <EOL> for real_index in test_index : <EOL> train_stacker [ real_index ] = ( preds [ no ] ) <EOL> no += <NUM_LIT:1> <EOL> i += <NUM_LIT:1> <EOL> mean_auc /= n <EOL> print ( "<STR_LIT>" % ( mean_auc ) ) <EOL> print ( "<STR_LIT>" ) <EOL> printfilcsve ( np . array ( train_stacker ) , filename + "<STR_LIT>" ) <EOL> preds = bagged_set ( X , y , model , SEED , bagging , X_test , update_seed = True ) <EOL> printfilcsve ( np . array ( preds ) , filename + "<STR_LIT>" ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
