<s> """<STR_LIT>""" <EOL> import csv <EOL> import math <EOL> import os <EOL> import random <EOL> import numpy as np <EOL> from sklearn . cross_validation import * <EOL> from sklearn . decomposition import * <EOL> from sklearn . ensemble import * <EOL> from sklearn . feature_selection import * <EOL> from sklearn . grid_search import * <EOL> from sklearn . linear_model import * <EOL> from sklearn . metrics import * <EOL> from sklearn . preprocessing import * <EOL> from sklearn . pipeline import * <EOL> from sklearn . svm import * <EOL> from sklearn . tree import * <EOL> def ams ( s , b ) : <EOL> return math . sqrt ( <NUM_LIT:2> * ( ( s + b + <NUM_LIT:10> ) * math . log ( <NUM_LIT:1.0> + s / ( b + <NUM_LIT:10> ) ) - s ) ) <EOL> def get_ams_score ( W , Y , Y_pred ) : <EOL> s = W * ( Y == <NUM_LIT:1> ) * ( Y_pred == <NUM_LIT:1> ) <EOL> b = W * ( Y == <NUM_LIT:0> ) * ( Y_pred == <NUM_LIT:1> ) <EOL> s = np . sum ( s ) <EOL> b = np . sum ( b ) <EOL> return ams ( s , b ) <EOL> def single_model ( ) : <EOL> classifier = AdaBoostClassifier ( <EOL> n_estimators = <NUM_LIT:20> , <EOL> learning_rate = <NUM_LIT> , <EOL> base_estimator = ExtraTreesClassifier ( <EOL> n_estimators = <NUM_LIT> , <EOL> max_features = <NUM_LIT:30> , <EOL> max_depth = <NUM_LIT:12> , <EOL> min_samples_leaf = <NUM_LIT:100> , <EOL> min_samples_split = <NUM_LIT:100> , <EOL> verbose = <NUM_LIT:1> , <EOL> n_jobs = - <NUM_LIT:1> ) ) <EOL> return classifier <EOL> def preprocess ( X , X_test ) : <EOL> imputer = Imputer ( missing_values = - <NUM_LIT> , strategy = '<STR_LIT>' ) <EOL> X = imputer . fit_transform ( X ) <EOL> X_test = imputer . transform ( X_test ) <EOL> inv_log_cols = ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:9> , <NUM_LIT:10> , <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT:16> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> X_inv_log_cols = np . log ( <NUM_LIT:1> / ( <NUM_LIT:1> + X [ : , inv_log_cols ] ) ) <EOL> X = np . hstack ( ( X , X_inv_log_cols ) ) <EOL> X_test_inv_log_cols = np . log ( <NUM_LIT:1> / ( <NUM_LIT:1> + X_test [ : , inv_log_cols ] ) ) <EOL> X_test = np . hstack ( ( X_test , X_test_inv_log_cols ) ) <EOL> scaler = StandardScaler ( ) <EOL> X = scaler . fit_transform ( X ) <EOL> X_test = scaler . transform ( X_test ) <EOL> return X , X_test <EOL> def train_and_predict ( X , W , Y , X_test ) : <EOL> X , X_test = preprocess ( X , X_test ) <EOL> classifier = single_model ( ) <EOL> classifier . fit ( X , Y , sample_weight = W ) <EOL> Y_pred = classifier . predict_proba ( X ) [ : , <NUM_LIT:1> ] <EOL> Y_test_pred = classifier . predict_proba ( X_test ) [ : , <NUM_LIT:1> ] <EOL> signal_threshold = <NUM_LIT> <EOL> cut = np . percentile ( Y_test_pred , signal_threshold ) <EOL> thresholded_Y_pred = Y_pred > cut <EOL> thresholded_Y_test_pred = Y_test_pred > cut <EOL> return [ Y_test_pred , thresholded_Y_test_pred ] <EOL> def write_submission_file ( ids_test , Y_test_pred , thresholded_Y_test_pred ) : <EOL> ids_probs = np . transpose ( np . vstack ( ( ids_test , Y_test_pred ) ) ) <EOL> ids_probs = np . array ( sorted ( ids_probs , key = lambda x : - x [ <NUM_LIT:1> ] ) ) <EOL> ids_probs_ranks = np . hstack ( ( <EOL> ids_probs , <EOL> np . arange ( <NUM_LIT:1> , ids_probs . shape [ <NUM_LIT:0> ] + <NUM_LIT:1> ) . reshape ( ( ids_probs . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) ) ) <EOL> test_ids_map = { } <EOL> for test_id , prob , rank in ids_probs_ranks : <EOL> test_id = int ( test_id ) <EOL> rank = int ( rank ) <EOL> test_ids_map [ test_id ] = rank <EOL> f = open ( '<STR_LIT>' % __file__ , '<STR_LIT:wb>' ) <EOL> writer = csv . writer ( f ) <EOL> writer . writerow ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> for i , pred in enumerate ( thresholded_Y_test_pred ) : <EOL> event_id = int ( ids_test [ i ] ) <EOL> rank = test_ids_map [ ids_test [ i ] ] <EOL> klass = pred and '<STR_LIT:s>' or '<STR_LIT:b>' <EOL> writer . writerow ( [ event_id , rank , klass ] ) <EOL> f . close ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> os . system ( '<STR_LIT>' % os . getpid ( ) ) <EOL> seed = <NUM_LIT> <EOL> random . seed ( seed ) <EOL> print '<STR_LIT>' <EOL> data = np . loadtxt ( '<STR_LIT>' , delimiter = '<STR_LIT:U+002C>' , skiprows = <NUM_LIT:1> , converters = { <NUM_LIT:32> : lambda x : int ( x == '<STR_LIT:s>' . encode ( '<STR_LIT:utf-8>' ) ) } ) <EOL> X = data [ : , <NUM_LIT:1> : <NUM_LIT> ] <EOL> Y = data [ : , <NUM_LIT:32> ] <EOL> W = data [ : , <NUM_LIT> ] <EOL> print '<STR_LIT>' <EOL> test_data = np . loadtxt ( '<STR_LIT>' , delimiter = '<STR_LIT:U+002C>' , skiprows = <NUM_LIT:1> ) <EOL> ids_test = test_data [ : , <NUM_LIT:0> ] <EOL> X_test = test_data [ : , <NUM_LIT:1> : <NUM_LIT> ] <EOL> W = data [ : , <NUM_LIT> ] <EOL> Y_test_pred , thresholded_Y_test_pred = train_and_predict ( X , W , Y , X_test ) <EOL> write_submission_file ( ids_test , Y_test_pred , thresholded_Y_test_pred ) </s>
