<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> from __future__ import absolute_import <EOL> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> import argparse <EOL> import os <EOL> import numpy as np <EOL> from sklearn . feature_extraction . text import CountVectorizer <EOL> from sklearn . feature_extraction . text import TfidfVectorizer <EOL> from sklearn . metrics import precision_score <EOL> from sklearn import linear_model <EOL> from sklearn import naive_bayes <EOL> from sklearn import tree <EOL> from sklearn import ensemble <EOL> from sklearn import svm <EOL> from sklearn import neighbors <EOL> from sklearn import cross_validation <EOL> from matplotlib import pyplot as plt <EOL> from nltk . corpus import stopwords <EOL> import unicodecsv <EOL> def reader ( class_name ) : <EOL> class_reader = unicodecsv . reader ( open ( class_name ) , encoding = '<STR_LIT:utf-8>' ) <EOL> row0 = next ( class_reader ) <EOL> assert row0 == [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> lines = [ ] <EOL> for tweet_id , tweet_text in class_reader : <EOL> txt = tweet_text . strip ( ) <EOL> if len ( txt ) > <NUM_LIT:0> : <EOL> lines . append ( txt ) <EOL> return lines <EOL> def cross_entropy_error ( Y , probas_ ) : <EOL> probas_class1 = probas_ [ : , <NUM_LIT:1> ] <EOL> probas_class1 [ np . where ( probas_class1 == <NUM_LIT:1.0> ) ] = <NUM_LIT> <EOL> probas_class1 [ np . where ( probas_class1 == <NUM_LIT:0.0> ) ] = <NUM_LIT> <EOL> cross_entropy_errors = ( ( - Y ) * ( np . log ( probas_class1 ) ) ) - ( ( <NUM_LIT:1> - Y ) * ( np . log ( <NUM_LIT:1> - probas_class1 ) ) ) <EOL> return cross_entropy_errors <EOL> def show_errors ( cross_entropy_errors_by_fold , method = "<STR_LIT>" , lower_is_better = True ) : <EOL> print ( "<STR_LIT>" % ( method ) + str ( cross_entropy_errors_by_fold ) ) <EOL> if lower_is_better : <EOL> note = "<STR_LIT>" <EOL> else : <EOL> note = "<STR_LIT>" <EOL> print ( "<STR_LIT>" % ( method , note , cross_entropy_errors_by_fold . mean ( ) , cross_entropy_errors_by_fold . std ( ) / <NUM_LIT:2> ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> parser = argparse . ArgumentParser ( description = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> data_dir = "<STR_LIT:data>" <EOL> in_class_name = os . path . join ( data_dir , args . table + '<STR_LIT>' ) <EOL> out_class_name = os . path . join ( data_dir , args . table + '<STR_LIT>' ) <EOL> in_class_lines = reader ( in_class_name ) <EOL> out_class_lines = reader ( out_class_name ) <EOL> train_set = np . array ( out_class_lines + in_class_lines ) <EOL> target = np . array ( [ <NUM_LIT:0> ] * len ( out_class_lines ) + [ <NUM_LIT:1> ] * len ( in_class_lines ) ) <EOL> stopWords = stopwords . words ( '<STR_LIT>' ) <EOL> MIN_DF = <NUM_LIT:2> <EOL> NGRAM_RANGE = ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> vectorizer_binary = CountVectorizer ( stop_words = stopWords , min_df = MIN_DF , binary = True , ngram_range = NGRAM_RANGE ) <EOL> vectorizer_tfidf = TfidfVectorizer ( stop_words = stopWords , min_df = MIN_DF , ngram_range = NGRAM_RANGE ) <EOL> vectorizer = vectorizer_binary <EOL> print ( vectorizer ) <EOL> _ = linear_model . LogisticRegression ( ) <EOL> _ = svm . LinearSVC ( ) <EOL> _ = naive_bayes . BernoulliNB ( ) <EOL> _ = naive_bayes . GaussianNB ( ) <EOL> _ = naive_bayes . MultinomialNB ( ) <EOL> _ = ensemble . AdaBoostClassifier ( n_estimators = <NUM_LIT:100> , base_estimator = tree . DecisionTreeClassifier ( max_depth = <NUM_LIT:2> , criterion = '<STR_LIT>' ) ) <EOL> _ = tree . DecisionTreeClassifier ( max_depth = <NUM_LIT:50> , min_samples_leaf = <NUM_LIT:5> ) <EOL> _ = ensemble . RandomForestClassifier ( max_depth = <NUM_LIT:10> , min_samples_leaf = <NUM_LIT:5> , n_estimators = <NUM_LIT:50> , n_jobs = - <NUM_LIT:1> , criterion = '<STR_LIT>' ) <EOL> clf = neighbors . KNeighborsClassifier ( n_neighbors = <NUM_LIT:11> ) <EOL> print ( clf ) <EOL> kf = cross_validation . KFold ( n = len ( target ) , n_folds = <NUM_LIT:5> , shuffle = True ) <EOL> f = plt . figure ( <NUM_LIT:1> ) <EOL> f . clf ( ) <EOL> cross_entropy_errors_test_by_fold = np . zeros ( len ( kf ) ) <EOL> cross_entropy_errors_train_by_fold = np . zeros ( len ( kf ) ) <EOL> precisions_by_fold = np . zeros ( len ( kf ) ) <EOL> probabilities_class_0_Y_test_all_folds = np . array ( [ ] ) <EOL> probabilities_class_1_Y_test_all_folds = np . array ( [ ] ) <EOL> all_false_positives_zipped = [ ] <EOL> for i , ( train_rows , test_rows ) in enumerate ( kf ) : <EOL> tweets_train_rows = train_set [ train_rows ] <EOL> tweets_test_rows = train_set [ test_rows ] <EOL> Y_train = target [ train_rows ] <EOL> Y_test = target [ test_rows ] <EOL> X_train = vectorizer . fit_transform ( tweets_train_rows ) . toarray ( ) <EOL> X_test = vectorizer . transform ( tweets_test_rows ) . todense ( ) <EOL> clf . fit ( X_train , Y_train ) <EOL> probas_test_ = clf . predict_proba ( X_test ) <EOL> predictions_test = clf . predict ( X_test ) <EOL> false_positive_locations = np . where ( Y_test - predictions_test == - <NUM_LIT:1> ) <EOL> false_positive_tweet_rows = test_rows [ np . where ( Y_test - predictions_test == - <NUM_LIT:1> ) ] <EOL> false_positive_tweets = train_set [ false_positive_tweet_rows ] <EOL> bag_of_words_false_positive_tweets = vectorizer . inverse_transform ( X_test [ false_positive_locations ] ) <EOL> false_positives_zipped = zip ( false_positive_tweets , bag_of_words_false_positive_tweets ) <EOL> all_false_positives_zipped . append ( false_positives_zipped ) <EOL> probabilities_class_1_Y_test = probas_test_ [ np . where ( Y_test == <NUM_LIT:1> ) ] <EOL> nbr_features_X_test = [ np . sum ( row ) for row in X_test [ np . where ( Y_test == <NUM_LIT:1> ) ] ] <EOL> class_1_labels = plt . scatter ( nbr_features_X_test , probabilities_class_1_Y_test [ : , <NUM_LIT:1> ] , c = '<STR_LIT:c>' , edgecolor = '<STR_LIT:none>' , label = "<STR_LIT>" ) <EOL> probabilities_class_0_Y_test = probas_test_ [ np . where ( Y_test == <NUM_LIT:0> ) ] <EOL> nbr_features_X_test = [ np . sum ( row ) for row in X_test [ np . where ( Y_test == <NUM_LIT:0> ) ] ] <EOL> class_0_labels = plt . scatter ( nbr_features_X_test , probabilities_class_0_Y_test [ : , <NUM_LIT:1> ] , c = '<STR_LIT:k>' , edgecolor = '<STR_LIT:none>' , label = "<STR_LIT>" ) <EOL> probas_train_ = clf . predict_proba ( X_train ) <EOL> if True : <EOL> cross_entropy_errors_test = cross_entropy_error ( Y_test , probas_test_ ) <EOL> cross_entropy_errors_train = cross_entropy_error ( Y_train , probas_train_ ) <EOL> cross_entropy_errors_test_by_fold [ i ] = np . average ( cross_entropy_errors_test ) <EOL> cross_entropy_errors_train_by_fold [ i ] = np . average ( cross_entropy_errors_train ) <EOL> precisions_by_fold [ i ] = precision_score ( Y_test , clf . predict ( X_test ) ) <EOL> print ( len ( test_rows ) ) <EOL> probabilities_class_0_Y_test_all_folds = np . concatenate ( ( probabilities_class_0_Y_test_all_folds , probabilities_class_0_Y_test [ : , <NUM_LIT:1> ] ) ) <EOL> probabilities_class_1_Y_test_all_folds = np . concatenate ( ( probabilities_class_1_Y_test_all_folds , probabilities_class_1_Y_test [ : , <NUM_LIT:1> ] ) ) <EOL> plt . legend ( ( class_1_labels , class_0_labels ) , ( class_1_labels . get_label ( ) , class_0_labels . get_label ( ) ) , scatterpoints = <NUM_LIT:2> , loc = <NUM_LIT:7> ) <EOL> plt . xlim ( xmin = - <NUM_LIT:1> ) <EOL> plt . ylim ( - <NUM_LIT> , <NUM_LIT> ) <EOL> plt . xlabel ( '<STR_LIT>' ) <EOL> plt . ylabel ( '<STR_LIT>' ) <EOL> plt . title ( "<STR_LIT>" . format ( str ( clf . __class__ ) . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] [ : - <NUM_LIT:2> ] , len ( vectorizer . get_feature_names ( ) ) ) ) <EOL> plt . show ( ) <EOL> if isinstance ( clf , tree . DecisionTreeClassifier ) : <EOL> feature_importances = zip ( clf . feature_importances_ , vectorizer . get_feature_names ( ) ) <EOL> feature_importances . sort ( reverse = True ) <EOL> print ( "<STR_LIT>" , feature_importances [ : <NUM_LIT:10> ] ) <EOL> with open ( "<STR_LIT>" , '<STR_LIT:w>' ) as f : <EOL> f = tree . export_graphviz ( clf , out_file = f , feature_names = vectorizer . get_feature_names ( ) ) <EOL> os . system ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> show_errors ( cross_entropy_errors_train_by_fold ) <EOL> print ( "<STR_LIT>" ) <EOL> show_errors ( cross_entropy_errors_test_by_fold ) <EOL> print ( "<STR_LIT>" ) <EOL> show_errors ( precisions_by_fold , method = "<STR_LIT>" , lower_is_better = False ) </s>
