#NEWLINE# def _create_learner_result_dicts( task_results , grid_scores , learner_result_dict_base ) : #NEWLINE# #INDENT# '\n    Create the learner result dictionaries that are used to create JSON and\n    plain-text results files.\n    ' #NEWLINE# res = [ ] #NEWLINE# num_folds = len ( task_results ) #NEWLINE# accuracy_sum = 0.0 #NEWLINE# pearson_sum = 0.0 #NEWLINE# score_sum = None #NEWLINE# prec_sum_dict = defaultdict ( float ) #NEWLINE# recall_sum_dict = defaultdict ( float ) #NEWLINE# f_sum_dict = defaultdict ( float ) #NEWLINE# result_table = None #NEWLINE# for ( k , ( ( conf_matrix , fold_accuracy , result_dict , model_params , score ) , grid_score ) ) in enumerate ( zip ( task_results , grid_scores ) , start = 1 ) : #NEWLINE# #INDENT# learner_result_dict = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# learner_result_dict . update ( learner_result_dict_base ) #NEWLINE# learner_result_dict [ 'result_table' ] = '' #NEWLINE# learner_result_dict [ 'accuracy' ] = '' #NEWLINE# learner_result_dict [ 'pearson' ] = '' #NEWLINE# learner_result_dict [ 'score' ] = '' #NEWLINE# learner_result_dict [ 'fold' ] = '' #NEWLINE# if ( learner_result_dict_base [ 'task' ] == 'cross_validate' ) : #NEWLINE# #INDENT# learner_result_dict [ 'fold' ] = k #NEWLINE# #UNINDENT# learner_result_dict [ 'model_params' ] = json . dumps ( model_params ) #NEWLINE# if ( grid_score is not None ) : #NEWLINE# #INDENT# learner_result_dict [ 'grid_score' ] = grid_score #NEWLINE# #UNINDENT# if conf_matrix : #NEWLINE# #INDENT# labels = sorted ( iterkeys ( task_results [ 0 ] [ 2 ] ) ) #NEWLINE# result_table = PrettyTable ( ( ( [ '' ] + labels ) + [ 'Precision' , 'Recall' , 'F-measure' ] ) , header = True , hrules = ALL ) #NEWLINE# result_table . align = 'r' #NEWLINE# result_table . float_format = '.3' #NEWLINE# for ( i , actual_label ) in enumerate ( labels ) : #NEWLINE# #INDENT# conf_matrix [ i ] [ i ] = '[{}]' . format ( conf_matrix [ i ] [ i ] ) #NEWLINE# label_prec = _get_stat_float ( result_dict [ actual_label ] , 'Precision' ) #NEWLINE# label_recall = _get_stat_float ( result_dict [ actual_label ] , 'Recall' ) #NEWLINE# label_f = _get_stat_float ( result_dict [ actual_label ] , 'F-measure' ) #NEWLINE# if ( not math . isnan ( label_prec ) ) : #NEWLINE# #INDENT# prec_sum_dict [ actual_label ] += float ( label_prec ) #NEWLINE# #UNINDENT# if ( not math . isnan ( label_recall ) ) : #NEWLINE# #INDENT# recall_sum_dict [ actual_label ] += float ( label_recall ) #NEWLINE# #UNINDENT# if ( not math . isnan ( label_f ) ) : #NEWLINE# #INDENT# f_sum_dict [ actual_label ] += float ( label_f ) #NEWLINE# #UNINDENT# result_row = ( ( [ actual_label ] + conf_matrix [ i ] ) + [ label_prec , label_recall , label_f ] ) #NEWLINE# result_table . add_row ( result_row ) #NEWLINE# #UNINDENT# result_table_str = '{}' . format ( result_table ) #NEWLINE# result_table_str += '\n(row = reference; column = predicted)' #NEWLINE# learner_result_dict [ 'result_table' ] = result_table_str #NEWLINE# learner_result_dict [ 'accuracy' ] = fold_accuracy #NEWLINE# accuracy_sum += fold_accuracy #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# learner_result_dict . update ( result_dict ) #NEWLINE# pearson_sum += float ( learner_result_dict [ 'pearson' ] ) #NEWLINE# #UNINDENT# if ( score is not None ) : #NEWLINE# #INDENT# if ( score_sum is None ) : #NEWLINE# #INDENT# score_sum = score #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# score_sum += score #NEWLINE# #UNINDENT# learner_result_dict [ 'score' ] = score #NEWLINE# #UNINDENT# res . append ( learner_result_dict ) #NEWLINE# #UNINDENT# if ( num_folds > 1 ) : #NEWLINE# #INDENT# learner_result_dict = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# learner_result_dict . update ( learner_result_dict_base ) #NEWLINE# learner_result_dict [ 'fold' ] = 'average' #NEWLINE# if result_table : #NEWLINE# #INDENT# result_table = PrettyTable ( [ 'Label' , 'Precision' , 'Recall' , 'F-measure' ] , header = True ) #NEWLINE# result_table . align = 'r' #NEWLINE# result_table . align [ 'Label' ] = 'l' #NEWLINE# result_table . float_format = '.3' #NEWLINE# for actual_label in labels : #NEWLINE# #INDENT# prec_mean = ( prec_sum_dict [ actual_label ] / num_folds ) #NEWLINE# recall_mean = ( recall_sum_dict [ actual_label ] / num_folds ) #NEWLINE# f_mean = ( f_sum_dict [ actual_label ] / num_folds ) #NEWLINE# result_table . add_row ( ( [ actual_label ] + [ prec_mean , recall_mean , f_mean ] ) ) #NEWLINE# #UNINDENT# learner_result_dict [ 'result_table' ] = '{}' . format ( result_table ) #NEWLINE# learner_result_dict [ 'accuracy' ] = ( accuracy_sum / num_folds ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# learner_result_dict [ 'pearson' ] = ( pearson_sum / num_folds ) #NEWLINE# #UNINDENT# if ( score_sum is not None ) : #NEWLINE# #INDENT# learner_result_dict [ 'score' ] = ( score_sum / num_folds ) #NEWLINE# #UNINDENT# res . append ( learner_result_dict ) #NEWLINE# #UNINDENT# return res