#NEWLINE# def _classify_featureset( args ) : #NEWLINE# #INDENT# ' Classification job to be submitted to grid ' #NEWLINE# experiment_name = args . pop ( 'experiment_name' ) #NEWLINE# task = args . pop ( 'task' ) #NEWLINE# sampler = args . pop ( 'sampler' ) #NEWLINE# feature_hasher = args . pop ( 'feature_hasher' ) #NEWLINE# hasher_features = args . pop ( 'hasher_features' ) #NEWLINE# job_name = args . pop ( 'job_name' ) #NEWLINE# featureset = args . pop ( 'featureset' ) #NEWLINE# featureset_name = args . pop ( 'featureset_name' ) #NEWLINE# learner_name = args . pop ( 'learner_name' ) #NEWLINE# train_path = args . pop ( 'train_path' ) #NEWLINE# test_path = args . pop ( 'test_path' ) #NEWLINE# train_set_name = args . pop ( 'train_set_name' ) #NEWLINE# test_set_name = args . pop ( 'test_set_name' ) #NEWLINE# shuffle = args . pop ( 'shuffle' ) #NEWLINE# model_path = args . pop ( 'model_path' ) #NEWLINE# prediction_prefix = args . pop ( 'prediction_prefix' ) #NEWLINE# grid_search = args . pop ( 'grid_search' ) #NEWLINE# grid_objective = args . pop ( 'grid_objective' ) #NEWLINE# suffix = args . pop ( 'suffix' ) #NEWLINE# log_path = args . pop ( 'log_path' ) #NEWLINE# probability = args . pop ( 'probability' ) #NEWLINE# results_path = args . pop ( 'results_path' ) #NEWLINE# fixed_parameters = args . pop ( 'fixed_parameters' ) #NEWLINE# sampler_parameters = args . pop ( 'sampler_parameters' ) #NEWLINE# param_grid = args . pop ( 'param_grid' ) #NEWLINE# pos_label_str = args . pop ( 'pos_label_str' ) #NEWLINE# overwrite = args . pop ( 'overwrite' ) #NEWLINE# feature_scaling = args . pop ( 'feature_scaling' ) #NEWLINE# min_feature_count = args . pop ( 'min_feature_count' ) #NEWLINE# grid_search_jobs = args . pop ( 'grid_search_jobs' ) #NEWLINE# grid_search_folds = args . pop ( 'grid_search_folds' ) #NEWLINE# cv_folds = args . pop ( 'cv_folds' ) #NEWLINE# save_cv_folds = args . pop ( 'save_cv_folds' ) #NEWLINE# stratified_folds = args . pop ( 'do_stratified_folds' ) #NEWLINE# label_col = args . pop ( 'label_col' ) #NEWLINE# id_col = args . pop ( 'id_col' ) #NEWLINE# ids_to_floats = args . pop ( 'ids_to_floats' ) #NEWLINE# class_map = args . pop ( 'class_map' ) #NEWLINE# custom_learner_path = args . pop ( 'custom_learner_path' ) #NEWLINE# quiet = args . pop ( 'quiet' , False ) #NEWLINE# if args : #NEWLINE# #INDENT# raise ValueError ( 'Extra arguments passed to _classify_featureset: {}' . format ( args . keys ( ) ) ) #NEWLINE# #UNINDENT# start_timestamp = datetime . datetime . now ( ) #NEWLINE# with open ( log_path , 'w' ) as log_file : #NEWLINE# #INDENT# print ( 'Task:' , task , file = log_file ) #NEWLINE# if ( task == 'cross_validate' ) : #NEWLINE# #INDENT# print ( ( 'Cross-validating ({} folds) on {}, feature ' + 'set {} ...' ) . format ( cv_folds , train_set_name , featureset ) , file = log_file ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( task == 'evaluate' ) : #NEWLINE# #INDENT# print ( ( 'Training on {}, Test on {}, ' + 'feature set {} ...' ) . format ( train_set_name , test_set_name , featureset ) , file = log_file ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( task == 'train' ) : #NEWLINE# #INDENT# print ( 'Training on {}, feature set {} ...' . format ( train_set_name , featureset ) , file = log_file ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# print ( ( 'Training on {}, Making predictions about {}, ' + 'feature set {} ...' ) . format ( train_set_name , test_set_name , featureset ) , file = log_file ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# modelfile = join ( model_path , '{}.model' . format ( job_name ) ) #NEWLINE# if ( ( task == 'cross_validate' ) or ( ( not exists ( modelfile ) ) or overwrite ) ) : #NEWLINE# #INDENT# train_examples = _load_featureset ( train_path , featureset , suffix , label_col = label_col , id_col = id_col , ids_to_floats = ids_to_floats , quiet = quiet , class_map = class_map , feature_hasher = feature_hasher , num_features = hasher_features ) #NEWLINE# train_set_size = len ( train_examples . ids ) #NEWLINE# if ( not train_examples . has_labels ) : #NEWLINE# #INDENT# raise ValueError ( 'Training examples do not have labels' ) #NEWLINE# #UNINDENT# learner = Learner ( learner_name , probability = probability , feature_scaling = feature_scaling , model_kwargs = fixed_parameters , pos_label_str = pos_label_str , min_feature_count = min_feature_count , sampler = sampler , sampler_kwargs = sampler_parameters , custom_learner_path = custom_learner_path ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if custom_learner_path : #NEWLINE# #INDENT# _import_custom_learner ( custom_learner_path , learner_name ) #NEWLINE# #UNINDENT# train_set_size = 'unknown' #NEWLINE# if ( exists ( modelfile ) and ( not overwrite ) ) : #NEWLINE# #INDENT# print ( ( '\tloading pre-existing %s model: %s' % ( learner_name , modelfile ) ) ) #NEWLINE# #UNINDENT# learner = Learner . from_file ( modelfile ) #NEWLINE# #UNINDENT# if ( ( task == 'evaluate' ) or ( task == 'predict' ) ) : #NEWLINE# #INDENT# test_examples = _load_featureset ( test_path , featureset , suffix , label_col = label_col , id_col = id_col , ids_to_floats = ids_to_floats , quiet = quiet , class_map = class_map , feature_hasher = feature_hasher , num_features = hasher_features ) #NEWLINE# test_set_size = len ( test_examples . ids ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# test_set_size = 'n/a' #NEWLINE# #UNINDENT# learner_result_dict_base = { #NEWLINE# #INDENT# 'experiment_name' : experiment_name , #NEWLINE# 'train_set_name' : train_set_name , #NEWLINE# 'train_set_size' : train_set_size , #NEWLINE# 'test_set_name' : test_set_name , #NEWLINE# 'test_set_size' : test_set_size , #NEWLINE# 'featureset' : json . dumps ( featureset ) , #NEWLINE# 'featureset_name' : featureset_name , #NEWLINE# 'shuffle' : shuffle , #NEWLINE# 'learner_name' : learner_name , #NEWLINE# 'task' : task , #NEWLINE# 'start_timestamp' : start_timestamp . strftime ( '%d %b %Y %H:%M:%S.%f' ) , #NEWLINE# 'version' : __version__ , #NEWLINE# 'feature_scaling' : feature_scaling , #NEWLINE# 'grid_search' : grid_search , #NEWLINE# 'grid_objective' : grid_objective , #NEWLINE# 'grid_search_folds' : grid_search_folds , #NEWLINE# 'min_feature_count' : min_feature_count , #NEWLINE# 'cv_folds' : cv_folds , #NEWLINE# 'save_cv_folds' : save_cv_folds , #NEWLINE# 'stratified_folds' : stratified_folds , #NEWLINE# 'scikit_learn_version' : SCIKIT_VERSION , #NEWLINE# #UNINDENT# } #NEWLINE# task_results = None #NEWLINE# if ( task == 'cross_validate' ) : #NEWLINE# #INDENT# print ( '\tcross-validating' , file = log_file ) #NEWLINE# ( task_results , grid_scores , skll_fold_ids ) = learner . cross_validate ( train_examples , shuffle = shuffle , stratified = stratified_folds , prediction_prefix = prediction_prefix , grid_search = grid_search , grid_search_folds = grid_search_folds , cv_folds = cv_folds , grid_objective = grid_objective , param_grid = param_grid , grid_jobs = grid_search_jobs , save_cv_folds = save_cv_folds ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( not exists ( modelfile ) ) or overwrite ) : #NEWLINE# #INDENT# print ( ( '\tfeaturizing and training new ' + '{} model' ) . format ( learner_name ) , file = log_file ) #NEWLINE# if ( not isinstance ( cv_folds , int ) ) : #NEWLINE# #INDENT# grid_search_folds = cv_folds #NEWLINE# #UNINDENT# best_score = learner . train ( train_examples , shuffle = shuffle , grid_search = grid_search , grid_search_folds = grid_search_folds , grid_objective = grid_objective , param_grid = param_grid , grid_jobs = grid_search_jobs ) #NEWLINE# grid_scores = [ best_score ] #NEWLINE# if model_path : #NEWLINE# #INDENT# learner . save ( modelfile ) #NEWLINE# #UNINDENT# if grid_search : #NEWLINE# #INDENT# print ( '\tbest {} grid search score: {}' . format ( grid_objective , round ( best_score , 3 ) ) , file = log_file ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# grid_scores = [ None ] #NEWLINE# #UNINDENT# param_out = ( '{}: {}' . format ( param_name , param_value ) for ( param_name , param_value ) in iteritems ( learner . model . get_params ( ) ) ) #NEWLINE# print ( '\thyperparameters: {}' . format ( ', ' . join ( param_out ) ) , file = log_file ) #NEWLINE# if ( task == 'evaluate' ) : #NEWLINE# #INDENT# print ( '\tevaluating predictions' , file = log_file ) #NEWLINE# task_results = [ learner . evaluate ( test_examples , prediction_prefix = prediction_prefix , grid_objective = grid_objective ) ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( task == 'predict' ) : #NEWLINE# #INDENT# print ( '\twriting predictions' , file = log_file ) #NEWLINE# learner . predict ( test_examples , prediction_prefix = prediction_prefix ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# end_timestamp = datetime . datetime . now ( ) #NEWLINE# learner_result_dict_base [ 'end_timestamp' ] = end_timestamp . strftime ( '%d %b %Y %H:%M:%S.%f' ) #NEWLINE# total_time = ( end_timestamp - start_timestamp ) #NEWLINE# learner_result_dict_base [ 'total_time' ] = str ( total_time ) #NEWLINE# if ( ( task == 'cross_validate' ) or ( task == 'evaluate' ) ) : #NEWLINE# #INDENT# results_json_path = join ( results_path , '{}.results.json' . format ( job_name ) ) #NEWLINE# res = _create_learner_result_dicts ( task_results , grid_scores , learner_result_dict_base ) #NEWLINE# file_mode = ( 'w' if ( sys . version_info >= ( 3 , 0 ) ) else 'wb' ) #NEWLINE# with open ( results_json_path , file_mode ) as json_file : #NEWLINE# #INDENT# json . dump ( res , json_file , cls = NumpyTypeEncoder ) #NEWLINE# #UNINDENT# with open ( join ( results_path , '{}.results' . format ( job_name ) ) , 'w' ) as output_file : #NEWLINE# #INDENT# _print_fancy_output ( res , output_file ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# res = [ learner_result_dict_base ] #NEWLINE# #UNINDENT# if ( ( task == 'cross_validate' ) and save_cv_folds ) : #NEWLINE# #INDENT# skll_fold_ids_file = ( experiment_name + '_skll_fold_ids.csv' ) #NEWLINE# file_mode = ( 'w' if ( sys . version_info >= ( 3 , 0 ) ) else 'wb' ) #NEWLINE# with open ( join ( results_path , skll_fold_ids_file ) , file_mode ) as output_file : #NEWLINE# #INDENT# _write_skll_folds ( skll_fold_ids , output_file ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return res