<s> import graphlab as gl <EOL> import re <EOL> import random <EOL> from copy import copy <EOL> import os <EOL> import graphlab . aggregate as agg <EOL> import array <EOL> import sys <EOL> model_path = "<STR_LIT>" <EOL> train_sf = [ ] <EOL> test_sf = [ ] <EOL> feature_names = [ ] <EOL> each_sf_feature_names = [ ] <EOL> for n in [ <NUM_LIT:0> ] : <EOL> try : <EOL> print "<STR_LIT>" % str ( n ) <EOL> Xf_train = gl . SFrame ( model_path + "<STR_LIT>" ) <EOL> Xf_test = gl . SFrame ( model_path + "<STR_LIT>" ) <EOL> sf_feature_names = [ ] <EOL> for fn in Xf_train . column_names ( ) : <EOL> if fn . startswith ( "<STR_LIT>" ) : <EOL> key = fn <EOL> idx = <NUM_LIT:0> <EOL> while key in feature_names : <EOL> key = fn + "<STR_LIT>" % idx <EOL> idx += <NUM_LIT:1> <EOL> if key != fn : <EOL> Xf_train . rename ( { fn : key } ) <EOL> Xf_test . rename ( { fn : key } ) <EOL> sf_feature_names . append ( key ) <EOL> train_sf . append ( Xf_train ) <EOL> test_sf . append ( Xf_test ) <EOL> each_sf_feature_names . append ( sf_feature_names ) <EOL> feature_names += sf_feature_names <EOL> except IOError , ier : <EOL> print "<STR_LIT>" % str ( n ) , "<STR_LIT>" , str ( ier ) <EOL> fn_path = "<STR_LIT>" <EOL> if False and os . path . exists ( fn ) : <EOL> X_train = gl . SFrame ( fn_path + "<STR_LIT>" ) <EOL> X_test = gl . SFrame ( fn_path + "<STR_LIT>" ) <EOL> else : <EOL> X_train = train_sf [ <NUM_LIT:0> ] [ [ "<STR_LIT:name>" , "<STR_LIT>" ] ] <EOL> X_test = test_sf [ <NUM_LIT:0> ] [ [ "<STR_LIT:name>" ] ] <EOL> for i , ( tr_sf , te_sf , fnl ) in enumerate ( zip ( train_sf , test_sf , each_sf_feature_names ) ) : <EOL> tr_2 , tr_valid = tr_sf . random_split ( <NUM_LIT> ) <EOL> print "<STR_LIT>" % ( i , len ( fnl ) ) <EOL> print fnl <EOL> m = gl . regression . linear_regression . create ( <EOL> tr_2 , target = "<STR_LIT>" , <EOL> features = fnl , <EOL> max_iterations = <NUM_LIT:100> , <EOL> validation_set = tr_valid , l2_penalty = <NUM_LIT> , solver = '<STR_LIT>' ) <EOL> pn = '<STR_LIT>' % i <EOL> tr_sf [ pn ] = m . predict ( tr_sf ) <EOL> te_sf [ pn ] = m . predict ( te_sf ) <EOL> score_feature = [ f for f in fnl if f . startswith ( '<STR_LIT>' ) ] <EOL> X_train = X_train . join ( tr_sf [ [ "<STR_LIT:name>" , pn ] + score_feature ] , on = "<STR_LIT:name>" ) <EOL> X_test = X_test . join ( te_sf [ [ "<STR_LIT:name>" , pn ] + score_feature ] , on = "<STR_LIT:name>" ) <EOL> X_train . save ( "<STR_LIT>" ) <EOL> X_test . save ( "<STR_LIT>" ) <EOL> import numpy as np <EOL> def pred_median ( d ) : <EOL> return np . median ( [ v for k , v in d . iteritems ( ) if k . startswith ( '<STR_LIT:L>' ) ] ) <EOL> def pred_max ( d ) : <EOL> return max ( v for k , v in d . iteritems ( ) if k . startswith ( '<STR_LIT:L>' ) ) <EOL> def pred_sorted ( d ) : <EOL> return dict ( ( i , v ) for i , ( v , k ) in enumerate ( <EOL> sorted ( ( v , k ) for k , v in d . iteritems ( ) if k . startswith ( '<STR_LIT:L>' ) ) ) ) <EOL> X_train [ '<STR_LIT>' ] = X_train . apply ( pred_median ) <EOL> X_test [ '<STR_LIT>' ] = X_test . apply ( pred_median ) <EOL> X_train [ '<STR_LIT>' ] = X_train . apply ( pred_max ) <EOL> X_test [ '<STR_LIT>' ] = X_test . apply ( pred_max ) <EOL> X_train [ '<STR_LIT>' ] = X_train . apply ( pred_sorted ) <EOL> X_test [ '<STR_LIT>' ] = X_test . apply ( pred_sorted ) <EOL> X_train_2 , X_valid = X_train . random_split ( <NUM_LIT> ) <EOL> prediction_method = "<STR_LIT>" <EOL> features = X_train . column_names ( ) <EOL> del features [ features . index ( "<STR_LIT:name>" ) ] <EOL> del features [ features . index ( "<STR_LIT>" ) ] <EOL> if prediction_method == "<STR_LIT>" : <EOL> m = gl . regression . linear_regression . create ( <EOL> X_train_2 , target = "<STR_LIT>" , <EOL> features = features , <EOL> max_iterations = <NUM_LIT:100> , <EOL> validation_set = X_valid , <EOL> solver = '<STR_LIT>' ) <EOL> X_test [ '<STR_LIT>' ] = m . predict ( X_test ) . apply ( lambda x : min ( <NUM_LIT:4> , max ( <NUM_LIT:0> , int ( round ( x ) ) ) ) ) <EOL> elif prediction_method == "<STR_LIT>" : <EOL> m = gl . regression . boosted_trees_regression . create ( <EOL> X_train , target = "<STR_LIT>" , <EOL> features = features , <EOL> max_iterations = <NUM_LIT:200> , <EOL> max_depth = <NUM_LIT:2> , <EOL> column_subsample = <NUM_LIT:1> , <EOL> row_subsample = <NUM_LIT:0.1> , <EOL> step_size = <NUM_LIT> ) <EOL> X_test [ '<STR_LIT>' ] = m . predict ( X_test ) . apply ( lambda x : min ( <NUM_LIT:4> , max ( <NUM_LIT:0> , int ( round ( x ) ) ) ) ) <EOL> elif prediction_method == "<STR_LIT>" : <EOL> X_train [ '<STR_LIT>' ] = X_train [ '<STR_LIT>' ] <EOL> X_test [ '<STR_LIT>' ] = X_test [ '<STR_LIT>' ] <EOL> X_out = X_test [ [ '<STR_LIT:name>' , '<STR_LIT>' ] ] <EOL> def get_number ( s ) : <EOL> n = float ( re . match ( '<STR_LIT>' , s ) . group ( <NUM_LIT:0> ) ) <EOL> if '<STR_LIT:right>' in s : <EOL> n += <NUM_LIT:0.5> <EOL> return n <EOL> X_out [ '<STR_LIT>' ] = X_out [ '<STR_LIT:name>' ] . apply ( get_number ) <EOL> X_out = X_out . sort ( '<STR_LIT>' ) <EOL> X_out . rename ( { "<STR_LIT:name>" : "<STR_LIT:image>" } ) <EOL> import csv <EOL> import time <EOL> with open ( '<STR_LIT>' % int ( time . time ( ) ) , '<STR_LIT:wb>' ) as outfile : <EOL> fieldnames = [ '<STR_LIT:image>' , '<STR_LIT>' ] <EOL> writer = csv . DictWriter ( outfile , fieldnames = fieldnames ) <EOL> writer . writeheader ( ) <EOL> for d in X_out [ [ '<STR_LIT:image>' , '<STR_LIT>' ] ] : <EOL> writer . writerow ( d ) </s>
