<s> import cPickle as pickle <EOL> import datasets , config <EOL> import numpy as np <EOL> import pandas as pd <EOL> from sklearn . externals import joblib <EOL> def load_from_cache ( filename ) : <EOL> filename = '<STR_LIT>' % ( config . CACHEDIR , filename ) <EOL> if config . CACHETYPE == '<STR_LIT>' : <EOL> obj = joblib . load ( filename ) <EOL> elif config . CACHETYPE == '<STR_LIT>' : <EOL> f = open ( filename ) <EOL> obj = pickle . load ( f ) <EOL> f . close ( ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % config . CACHETYPE ) <EOL> return obj <EOL> def save_to_cache ( obj , filename ) : <EOL> filename = '<STR_LIT>' % ( config . CACHEDIR , filename ) <EOL> if config . CACHETYPE == '<STR_LIT>' : <EOL> joblib . dump ( obj , filename , compress = <NUM_LIT:9> ) <EOL> elif config . CACHETYPE == '<STR_LIT>' : <EOL> f = open ( '<STR_LIT>' % filename , '<STR_LIT:w>' ) <EOL> pickle . dump ( obj , f , <NUM_LIT:2> ) <EOL> f . close ( ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % config . CACHETYPE ) <EOL> def create_submission ( filename , pred , ids = None ) : <EOL> data = [ '<STR_LIT>' ] <EOL> pred = pred . astype ( '<STR_LIT>' ) <EOL> if ids is None : <EOL> ids = datasets . load_dataset ( '<STR_LIT>' ) <EOL> for id , p in zip ( ids , pred ) : <EOL> data . append ( '<STR_LIT>' % ( id ) + '<STR_LIT:U+002C>' . join ( p ) ) <EOL> data = '<STR_LIT:\n>' . join ( data ) <EOL> f = open ( '<STR_LIT>' % ( config . SUBMITDIR , filename ) , '<STR_LIT:w>' ) <EOL> f . write ( data ) <EOL> f . close ( ) <EOL> def make_vw ( data , targets , filename ) : <EOL> """<STR_LIT>""" <EOL> s = [ ] <EOL> for yi , xi in zip ( targets , data ) : <EOL> xis = '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' % ( f , xi [ <NUM_LIT:0> , f ] ) for f in xi . nonzero ( ) [ <NUM_LIT:1> ] ] ) <EOL> s . append ( '<STR_LIT>' % ( yi , xis ) ) <EOL> f = open ( filename , '<STR_LIT:w>' ) <EOL> f . write ( '<STR_LIT:\n>' . join ( s ) ) <EOL> f . close ( ) <EOL> def greedy_feature_selection ( model , features , j ) : <EOL> selected_features = set ( ) <EOL> score_hist = [ ] <EOL> ycv = exp ( y_cv ) - <NUM_LIT:1> <EOL> while len ( selected_features ) < len ( features ) : <EOL> scores = [ ] <EOL> for i in range ( len ( features ) ) : <EOL> if i not in selected_features : <EOL> feats = list ( selected_features ) + [ i ] <EOL> if len ( feats ) == <NUM_LIT:1> : <EOL> ttfs = features [ i ] <EOL> else : <EOL> ttfs = data_transforms . drop_disjoint ( sparse . hstack ( ( <EOL> features [ feats ] ) ) . tocsr ( ) , targets ) <EOL> X_train_pre = ttfs [ : n_train ] <EOL> X_train = X_train_pre [ : int ( n_train * <NUM_LIT> ) ] <EOL> X_cv = X_train_pre [ int ( n_train * <NUM_LIT> ) : ] <EOL> model . fit ( X_train [ - keep : ] , y_train [ - keep : ] ) <EOL> cv = exp ( ridge . predict ( X_cv ) ) - <NUM_LIT:1> <EOL> scores . append ( ( rmsle ( postprocess_pred ( cv ) [ : , j ] , ycv [ : , j ] ) , feats , i ) ) <EOL> print scores [ - <NUM_LIT:1> ] <EOL> selected_features . add ( min ( scores ) [ <NUM_LIT:2> ] ) <EOL> score_hist . append ( min ( scores ) ) <EOL> return score_hist </s>
