#NEWLINE# def train_model( clf , X , Y , name = 'NB ngram' , plot = False ) : #NEWLINE# #INDENT# cv = ShuffleSplit ( n = len ( X ) , n_iter = 10 , test_size = 0.3 , indices = True , random_state = 0 ) #NEWLINE# train_errors = [ ] #NEWLINE# test_errors = [ ] #NEWLINE# scores = [ ] #NEWLINE# pr_scores = [ ] #NEWLINE# ( precisions , recalls , thresholds ) = ( [ ] , [ ] , [ ] ) #NEWLINE# for ( train , test ) in cv : #NEWLINE# #INDENT# ( X_train , y_train ) = ( X [ train ] , Y [ train ] ) #NEWLINE# ( X_test , y_test ) = ( X [ test ] , Y [ test ] ) #NEWLINE# clf . fit ( X_train , y_train ) #NEWLINE# train_score = clf . score ( X_train , y_train ) #NEWLINE# test_score = clf . score ( X_test , y_test ) #NEWLINE# train_errors . append ( ( 1 - train_score ) ) #NEWLINE# test_errors . append ( ( 1 - test_score ) ) #NEWLINE# scores . append ( test_score ) #NEWLINE# proba = clf . predict_proba ( X_test ) #NEWLINE# ( fpr , tpr , roc_thresholds ) = roc_curve ( y_test , proba [ : , 1 ] ) #NEWLINE# ( precision , recall , pr_thresholds ) = precision_recall_curve ( y_test , proba [ : , 1 ] ) #NEWLINE# pr_scores . append ( auc ( recall , precision ) ) #NEWLINE# precisions . append ( precision ) #NEWLINE# recalls . append ( recall ) #NEWLINE# thresholds . append ( pr_thresholds ) #NEWLINE# #UNINDENT# if plot : #NEWLINE# #INDENT# scores_to_sort = pr_scores #NEWLINE# median = np . argsort ( scores_to_sort ) [ ( len ( scores_to_sort ) / 2 ) ] #NEWLINE# plot_pr ( pr_scores [ median ] , name , phase , precisions [ median ] , recalls [ median ] , label = name ) #NEWLINE# #UNINDENT# summary = ( np . mean ( scores ) , np . std ( scores ) , np . mean ( pr_scores ) , np . std ( pr_scores ) ) #NEWLINE# print ( ( '%.3f\t%.3f\t%.3f\t%.3f\t' % summary ) ) #NEWLINE# return ( np . mean ( train_errors ) , np . mean ( test_errors ) )