<s> """<STR_LIT>""" <EOL> from __future__ import division , print_function <EOL> import os <EOL> from time import time <EOL> import argparse <EOL> import numpy as np <EOL> from sklearn . datasets import fetch_covtype , get_data_home <EOL> from sklearn . svm import LinearSVC <EOL> from sklearn . linear_model import SGDClassifier , LogisticRegression <EOL> from sklearn . naive_bayes import GaussianNB <EOL> from sklearn . tree import DecisionTreeClassifier <EOL> from sklearn . ensemble import RandomForestClassifier , ExtraTreesClassifier <EOL> from sklearn . ensemble import GradientBoostingClassifier <EOL> from sklearn . metrics import zero_one_loss <EOL> from sklearn . externals . joblib import Memory <EOL> from sklearn . utils import check_array <EOL> memory = Memory ( os . path . join ( get_data_home ( ) , '<STR_LIT>' ) , <EOL> mmap_mode = '<STR_LIT:r>' ) <EOL> @ memory . cache <EOL> def load_data ( dtype = np . float32 , order = '<STR_LIT:C>' , random_state = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> print ( "<STR_LIT>" ) <EOL> data = fetch_covtype ( download_if_missing = True , shuffle = True , <EOL> random_state = random_state ) <EOL> X = check_array ( data [ '<STR_LIT:data>' ] , dtype = dtype , order = order ) <EOL> y = ( data [ '<STR_LIT:target>' ] != <NUM_LIT:1> ) . astype ( np . int ) <EOL> print ( "<STR_LIT>" ) <EOL> n_train = <NUM_LIT> <EOL> X_train = X [ : n_train ] <EOL> y_train = y [ : n_train ] <EOL> X_test = X [ n_train : ] <EOL> y_test = y [ n_train : ] <EOL> mean = X_train . mean ( axis = <NUM_LIT:0> ) <EOL> std = X_train . std ( axis = <NUM_LIT:0> ) <EOL> mean [ <NUM_LIT:10> : ] = <NUM_LIT:0.0> <EOL> std [ <NUM_LIT:10> : ] = <NUM_LIT:1.0> <EOL> X_train = ( X_train - mean ) / std <EOL> X_test = ( X_test - mean ) / std <EOL> return X_train , X_test , y_train , y_test <EOL> ESTIMATORS = { <EOL> '<STR_LIT>' : GradientBoostingClassifier ( n_estimators = <NUM_LIT> ) , <EOL> '<STR_LIT>' : ExtraTreesClassifier ( n_estimators = <NUM_LIT:20> ) , <EOL> '<STR_LIT>' : RandomForestClassifier ( n_estimators = <NUM_LIT:20> ) , <EOL> '<STR_LIT>' : DecisionTreeClassifier ( min_samples_split = <NUM_LIT:5> ) , <EOL> '<STR_LIT>' : SGDClassifier ( alpha = <NUM_LIT> , n_iter = <NUM_LIT:2> ) , <EOL> '<STR_LIT>' : GaussianNB ( ) , <EOL> '<STR_LIT>' : LinearSVC ( loss = "<STR_LIT>" , penalty = "<STR_LIT>" , C = <NUM_LIT:1000> , dual = False , <EOL> tol = <NUM_LIT> ) , <EOL> '<STR_LIT>' : LogisticRegression ( solver = '<STR_LIT>' , max_iter = <NUM_LIT:2> , C = <NUM_LIT:1000> ) <EOL> } <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , nargs = "<STR_LIT:+>" , <EOL> choices = ESTIMATORS , type = str , <EOL> default = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , nargs = "<STR_LIT:?>" , default = <NUM_LIT:1> , type = int , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , nargs = "<STR_LIT:?>" , default = "<STR_LIT:C>" , type = str , <EOL> choices = [ "<STR_LIT:F>" , "<STR_LIT:C>" ] , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT:data>" ) <EOL> parser . add_argument ( '<STR_LIT>' , nargs = "<STR_LIT:?>" , default = <NUM_LIT> , type = int , <EOL> help = "<STR_LIT>" ) <EOL> args = vars ( parser . parse_args ( ) ) <EOL> print ( __doc__ ) <EOL> X_train , X_test , y_train , y_test = load_data ( <EOL> order = args [ "<STR_LIT>" ] , random_state = args [ "<STR_LIT>" ] ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" % ( "<STR_LIT>" . ljust ( <NUM_LIT> ) , X_train . shape [ <NUM_LIT:1> ] ) ) <EOL> print ( "<STR_LIT>" % ( "<STR_LIT>" . ljust ( <NUM_LIT> ) , np . unique ( y_train ) . size ) ) <EOL> print ( "<STR_LIT>" % ( "<STR_LIT>" . ljust ( <NUM_LIT> ) , X_train . dtype ) ) <EOL> print ( "<STR_LIT>" <EOL> % ( "<STR_LIT>" . ljust ( <NUM_LIT> ) , <EOL> X_train . shape [ <NUM_LIT:0> ] , np . sum ( y_train == <NUM_LIT:1> ) , <EOL> np . sum ( y_train == <NUM_LIT:0> ) , int ( X_train . nbytes / <NUM_LIT> ) ) ) <EOL> print ( "<STR_LIT>" <EOL> % ( "<STR_LIT>" . ljust ( <NUM_LIT> ) , <EOL> X_test . shape [ <NUM_LIT:0> ] , np . sum ( y_test == <NUM_LIT:1> ) , <EOL> np . sum ( y_test == <NUM_LIT:0> ) , int ( X_test . nbytes / <NUM_LIT> ) ) ) <EOL> print ( ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> error , train_time , test_time = { } , { } , { } <EOL> for name in sorted ( args [ "<STR_LIT>" ] ) : <EOL> print ( "<STR_LIT>" % name , end = "<STR_LIT>" ) <EOL> estimator = ESTIMATORS [ name ] <EOL> estimator_params = estimator . get_params ( ) <EOL> estimator . set_params ( ** { p : args [ "<STR_LIT>" ] <EOL> for p in estimator_params <EOL> if p . endswith ( "<STR_LIT>" ) } ) <EOL> if "<STR_LIT>" in estimator_params : <EOL> estimator . set_params ( n_jobs = args [ "<STR_LIT>" ] ) <EOL> time_start = time ( ) <EOL> estimator . fit ( X_train , y_train ) <EOL> train_time [ name ] = time ( ) - time_start <EOL> time_start = time ( ) <EOL> y_pred = estimator . predict ( X_test ) <EOL> test_time [ name ] = time ( ) - time_start <EOL> error [ name ] = zero_one_loss ( y_test , y_pred ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" <EOL> % ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> print ( "<STR_LIT:->" * <NUM_LIT> ) <EOL> for name in sorted ( args [ "<STR_LIT>" ] , key = error . get ) : <EOL> print ( "<STR_LIT>" % ( name . ljust ( <NUM_LIT:12> ) , <EOL> ( "<STR_LIT>" % train_time [ name ] ) . center ( <NUM_LIT:10> ) , <EOL> ( "<STR_LIT>" % test_time [ name ] ) . center ( <NUM_LIT:10> ) , <EOL> ( "<STR_LIT>" % error [ name ] ) . center ( <NUM_LIT:10> ) ) ) <EOL> print ( ) </s>
