<s> import os <EOL> import timeit <EOL> import numpy as np <EOL> from collections import defaultdict <EOL> from sklearn . metrics import roc_curve <EOL> from sklearn . metrics import auc <EOL> from sklearn . cross_validation import ShuffleSplit <EOL> from sklearn . linear_model . logistic import LogisticRegression <EOL> from sklearn . metrics import confusion_matrix <EOL> from sklearn . externals import joblib <EOL> from utils import GENRE_LIST , GENRE_DIR , TEST_DIR <EOL> from utils import plot_confusion_matrix , plot_roc_curves <EOL> from ceps import read_ceps , read_ceps_test <EOL> genre_list = GENRE_LIST <EOL> def train_model ( X , Y , name , plot = False ) : <EOL> """<STR_LIT>""" <EOL> labels = np . unique ( Y ) <EOL> cv = ShuffleSplit ( n = len ( X ) , n_iterations = <NUM_LIT:1> , test_fraction = <NUM_LIT> , indices = True , random_state = <NUM_LIT:0> ) <EOL> train_errors = [ ] <EOL> test_errors = [ ] <EOL> scores = [ ] <EOL> pr_scores = defaultdict ( list ) <EOL> precisions , recalls , thresholds = defaultdict ( list ) , defaultdict ( list ) , defaultdict ( list ) <EOL> roc_scores = defaultdict ( list ) <EOL> tprs = defaultdict ( list ) <EOL> fprs = defaultdict ( list ) <EOL> clfs = [ ] <EOL> cms = [ ] <EOL> for train , test in cv : <EOL> X_train , y_train = X [ train ] , Y [ train ] <EOL> X_test , y_test = X [ test ] , Y [ test ] <EOL> clf = LogisticRegression ( ) <EOL> clf . fit ( X_train , y_train ) <EOL> clfs . append ( clf ) <EOL> train_score = clf . score ( X_train , y_train ) <EOL> test_score = clf . score ( X_test , y_test ) <EOL> scores . append ( test_score ) <EOL> train_errors . append ( <NUM_LIT:1> - train_score ) <EOL> test_errors . append ( <NUM_LIT:1> - test_score ) <EOL> y_pred = clf . predict ( X_test ) <EOL> cm = confusion_matrix ( y_test , y_pred ) <EOL> cms . append ( cm ) <EOL> for label in labels : <EOL> y_label_test = np . asarray ( y_test == label , dtype = int ) <EOL> proba = clf . predict_proba ( X_test ) <EOL> proba_label = proba [ : , label ] <EOL> fpr , tpr , roc_thresholds = roc_curve ( y_label_test , proba_label ) <EOL> roc_scores [ label ] . append ( auc ( fpr , tpr ) ) <EOL> tprs [ label ] . append ( tpr ) <EOL> fprs [ label ] . append ( fpr ) <EOL> if plot : <EOL> for label in labels : <EOL> scores_to_sort = roc_scores [ label ] <EOL> median = np . argsort ( scores_to_sort ) [ len ( scores_to_sort ) / <NUM_LIT:2> ] <EOL> desc = "<STR_LIT>" % ( name , genre_list [ label ] ) <EOL> plot_roc_curves ( roc_scores [ label ] [ median ] , desc , tprs [ label ] [ median ] , fprs [ label ] [ median ] , label = '<STR_LIT>' % genre_list [ label ] ) <EOL> all_pr_scores = np . asarray ( pr_scores . values ( ) ) . flatten ( ) <EOL> summary = ( np . mean ( scores ) , np . std ( scores ) , np . mean ( all_pr_scores ) , np . std ( all_pr_scores ) ) <EOL> joblib . dump ( clf , '<STR_LIT>' ) <EOL> return np . mean ( train_errors ) , np . mean ( test_errors ) , np . asarray ( cms ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> start = timeit . default_timer ( ) <EOL> print <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> X , y = read_ceps ( genre_list ) <EOL> train_avg , test_avg , cms = train_model ( X , y , "<STR_LIT>" , plot = True ) <EOL> cm_avg = np . mean ( cms , axis = <NUM_LIT:0> ) <EOL> cm_norm = cm_avg / np . sum ( cm_avg , axis = <NUM_LIT:0> ) <EOL> print "<STR_LIT>" <EOL> stop = timeit . default_timer ( ) <EOL> print "<STR_LIT>" , ( stop - start ) <EOL> print "<STR_LIT>" <EOL> plot_confusion_matrix ( cm_norm , genre_list , "<STR_LIT>" , "<STR_LIT>" ) <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" </s>
