<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import logging <EOL> import numpy as np <EOL> from optparse import OptionParser <EOL> import sys <EOL> from time import time <EOL> import matplotlib . pyplot as plt <EOL> from sklearn . datasets import fetch_20newsgroups <EOL> from sklearn . feature_extraction . text import TfidfVectorizer <EOL> from sklearn . feature_extraction . text import HashingVectorizer <EOL> from sklearn . feature_selection import SelectKBest , chi2 <EOL> from sklearn . linear_model import RidgeClassifier <EOL> from sklearn . pipeline import Pipeline <EOL> from sklearn . svm import LinearSVC <EOL> from sklearn . linear_model import SGDClassifier <EOL> from sklearn . linear_model import Perceptron <EOL> from sklearn . linear_model import PassiveAggressiveClassifier <EOL> from sklearn . naive_bayes import BernoulliNB , MultinomialNB <EOL> from sklearn . neighbors import KNeighborsClassifier <EOL> from sklearn . neighbors import NearestCentroid <EOL> from sklearn . ensemble import RandomForestClassifier <EOL> from sklearn . utils . extmath import density <EOL> from sklearn import metrics <EOL> logging . basicConfig ( level = logging . INFO , <EOL> format = '<STR_LIT>' ) <EOL> op = OptionParser ( ) <EOL> op . add_option ( "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> op . add_option ( "<STR_LIT>" , <EOL> action = "<STR_LIT:store>" , type = "<STR_LIT:int>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> op . add_option ( "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> op . add_option ( "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> op . add_option ( "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> op . add_option ( "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> op . add_option ( "<STR_LIT>" , <EOL> action = "<STR_LIT:store>" , type = int , default = <NUM_LIT:2> ** <NUM_LIT:16> , <EOL> help = "<STR_LIT>" ) <EOL> op . add_option ( "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ( opts , args ) = op . parse_args ( ) <EOL> if len ( args ) > <NUM_LIT:0> : <EOL> op . error ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> print ( __doc__ ) <EOL> op . print_help ( ) <EOL> print ( ) <EOL> if opts . all_categories : <EOL> categories = None <EOL> else : <EOL> categories = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> if opts . filtered : <EOL> remove = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> remove = ( ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( categories if categories else "<STR_LIT:all>" ) <EOL> data_train = fetch_20newsgroups ( subset = '<STR_LIT:train>' , categories = categories , <EOL> shuffle = True , random_state = <NUM_LIT> , <EOL> remove = remove ) <EOL> data_test = fetch_20newsgroups ( subset = '<STR_LIT:test>' , categories = categories , <EOL> shuffle = True , random_state = <NUM_LIT> , <EOL> remove = remove ) <EOL> print ( '<STR_LIT>' ) <EOL> categories = data_train . target_names <EOL> def size_mb ( docs ) : <EOL> return sum ( len ( s . encode ( '<STR_LIT:utf-8>' ) ) for s in docs ) / <NUM_LIT> <EOL> data_train_size_mb = size_mb ( data_train . data ) <EOL> data_test_size_mb = size_mb ( data_test . data ) <EOL> print ( "<STR_LIT>" % ( <EOL> len ( data_train . data ) , data_train_size_mb ) ) <EOL> print ( "<STR_LIT>" % ( <EOL> len ( data_test . data ) , data_test_size_mb ) ) <EOL> print ( "<STR_LIT>" % len ( categories ) ) <EOL> print ( ) <EOL> y_train , y_test = data_train . target , data_test . target <EOL> print ( "<STR_LIT>" ) <EOL> t0 = time ( ) <EOL> if opts . use_hashing : <EOL> vectorizer = HashingVectorizer ( stop_words = '<STR_LIT>' , non_negative = True , <EOL> n_features = opts . n_features ) <EOL> X_train = vectorizer . transform ( data_train . data ) <EOL> else : <EOL> vectorizer = TfidfVectorizer ( sublinear_tf = True , max_df = <NUM_LIT:0.5> , <EOL> stop_words = '<STR_LIT>' ) <EOL> X_train = vectorizer . fit_transform ( data_train . data ) <EOL> duration = time ( ) - t0 <EOL> print ( "<STR_LIT>" % ( duration , data_train_size_mb / duration ) ) <EOL> print ( "<STR_LIT>" % X_train . shape ) <EOL> print ( ) <EOL> print ( "<STR_LIT>" ) <EOL> t0 = time ( ) <EOL> X_test = vectorizer . transform ( data_test . data ) <EOL> duration = time ( ) - t0 <EOL> print ( "<STR_LIT>" % ( duration , data_test_size_mb / duration ) ) <EOL> print ( "<STR_LIT>" % X_test . shape ) <EOL> print ( ) <EOL> if opts . use_hashing : <EOL> feature_names = None <EOL> else : <EOL> feature_names = vectorizer . get_feature_names ( ) <EOL> if opts . select_chi2 : <EOL> print ( "<STR_LIT>" % <EOL> opts . select_chi2 ) <EOL> t0 = time ( ) <EOL> ch2 = SelectKBest ( chi2 , k = opts . select_chi2 ) <EOL> X_train = ch2 . fit_transform ( X_train , y_train ) <EOL> X_test = ch2 . transform ( X_test ) <EOL> if feature_names : <EOL> feature_names = [ feature_names [ i ] for i <EOL> in ch2 . get_support ( indices = True ) ] <EOL> print ( "<STR_LIT>" % ( time ( ) - t0 ) ) <EOL> print ( ) <EOL> if feature_names : <EOL> feature_names = np . asarray ( feature_names ) <EOL> def trim ( s ) : <EOL> """<STR_LIT>""" <EOL> return s if len ( s ) <= <NUM_LIT> else s [ : <NUM_LIT> ] + "<STR_LIT>" <EOL> def benchmark ( clf ) : <EOL> print ( '<STR_LIT:_>' * <NUM_LIT> ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( clf ) <EOL> t0 = time ( ) <EOL> clf . fit ( X_train , y_train ) <EOL> train_time = time ( ) - t0 <EOL> print ( "<STR_LIT>" % train_time ) <EOL> t0 = time ( ) <EOL> pred = clf . predict ( X_test ) <EOL> test_time = time ( ) - t0 <EOL> print ( "<STR_LIT>" % test_time ) <EOL> score = metrics . accuracy_score ( y_test , pred ) <EOL> print ( "<STR_LIT>" % score ) <EOL> if hasattr ( clf , '<STR_LIT>' ) : <EOL> print ( "<STR_LIT>" % clf . coef_ . shape [ <NUM_LIT:1> ] ) <EOL> print ( "<STR_LIT>" % density ( clf . coef_ ) ) <EOL> if opts . print_top10 and feature_names is not None : <EOL> print ( "<STR_LIT>" ) <EOL> for i , category in enumerate ( categories ) : <EOL> top10 = np . argsort ( clf . coef_ [ i ] ) [ - <NUM_LIT:10> : ] <EOL> print ( trim ( "<STR_LIT>" <EOL> % ( category , "<STR_LIT:U+0020>" . join ( feature_names [ top10 ] ) ) ) ) <EOL> print ( ) <EOL> if opts . print_report : <EOL> print ( "<STR_LIT>" ) <EOL> print ( metrics . classification_report ( y_test , pred , <EOL> target_names = categories ) ) <EOL> if opts . print_cm : <EOL> print ( "<STR_LIT>" ) <EOL> print ( metrics . confusion_matrix ( y_test , pred ) ) <EOL> print ( ) <EOL> clf_descr = str ( clf ) . split ( '<STR_LIT:(>' ) [ <NUM_LIT:0> ] <EOL> return clf_descr , score , train_time , test_time <EOL> results = [ ] <EOL> for clf , name in ( <EOL> ( RidgeClassifier ( tol = <NUM_LIT> , solver = "<STR_LIT>" ) , "<STR_LIT>" ) , <EOL> ( Perceptron ( n_iter = <NUM_LIT:50> ) , "<STR_LIT>" ) , <EOL> ( PassiveAggressiveClassifier ( n_iter = <NUM_LIT:50> ) , "<STR_LIT>" ) , <EOL> ( KNeighborsClassifier ( n_neighbors = <NUM_LIT:10> ) , "<STR_LIT>" ) , <EOL> ( RandomForestClassifier ( n_estimators = <NUM_LIT:100> ) , "<STR_LIT>" ) ) : <EOL> print ( '<STR_LIT:=>' * <NUM_LIT> ) <EOL> print ( name ) <EOL> results . append ( benchmark ( clf ) ) <EOL> for penalty in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> print ( '<STR_LIT:=>' * <NUM_LIT> ) <EOL> print ( "<STR_LIT>" % penalty . upper ( ) ) <EOL> results . append ( benchmark ( LinearSVC ( loss = '<STR_LIT>' , penalty = penalty , <EOL> dual = False , tol = <NUM_LIT> ) ) ) <EOL> results . append ( benchmark ( SGDClassifier ( alpha = <NUM_LIT> , n_iter = <NUM_LIT:50> , <EOL> penalty = penalty ) ) ) <EOL> print ( '<STR_LIT:=>' * <NUM_LIT> ) <EOL> print ( "<STR_LIT>" ) <EOL> results . append ( benchmark ( SGDClassifier ( alpha = <NUM_LIT> , n_iter = <NUM_LIT:50> , <EOL> penalty = "<STR_LIT>" ) ) ) <EOL> print ( '<STR_LIT:=>' * <NUM_LIT> ) <EOL> print ( "<STR_LIT>" ) <EOL> results . append ( benchmark ( NearestCentroid ( ) ) ) <EOL> print ( '<STR_LIT:=>' * <NUM_LIT> ) <EOL> print ( "<STR_LIT>" ) <EOL> results . append ( benchmark ( MultinomialNB ( alpha = <NUM_LIT> ) ) ) <EOL> results . append ( benchmark ( BernoulliNB ( alpha = <NUM_LIT> ) ) ) <EOL> print ( '<STR_LIT:=>' * <NUM_LIT> ) <EOL> print ( "<STR_LIT>" ) <EOL> results . append ( benchmark ( Pipeline ( [ <EOL> ( '<STR_LIT>' , LinearSVC ( penalty = "<STR_LIT>" , dual = False , tol = <NUM_LIT> ) ) , <EOL> ( '<STR_LIT>' , LinearSVC ( ) ) <EOL> ] ) ) ) <EOL> indices = np . arange ( len ( results ) ) <EOL> results = [ [ x [ i ] for x in results ] for i in range ( <NUM_LIT:4> ) ] <EOL> clf_names , score , training_time , test_time = results <EOL> training_time = np . array ( training_time ) / np . max ( training_time ) <EOL> test_time = np . array ( test_time ) / np . max ( test_time ) <EOL> plt . figure ( figsize = ( <NUM_LIT:12> , <NUM_LIT:8> ) ) <EOL> plt . title ( "<STR_LIT>" ) <EOL> plt . barh ( indices , score , <NUM_LIT> , label = "<STR_LIT>" , color = '<STR_LIT>' ) <EOL> plt . barh ( indices + <NUM_LIT> , training_time , <NUM_LIT> , label = "<STR_LIT>" , <EOL> color = '<STR_LIT:c>' ) <EOL> plt . barh ( indices + <NUM_LIT> , test_time , <NUM_LIT> , label = "<STR_LIT>" , color = '<STR_LIT>' ) <EOL> plt . yticks ( ( ) ) <EOL> plt . legend ( loc = '<STR_LIT>' ) <EOL> plt . subplots_adjust ( left = <NUM_LIT> ) <EOL> plt . subplots_adjust ( top = <NUM_LIT> ) <EOL> plt . subplots_adjust ( bottom = <NUM_LIT> ) <EOL> for i , c in zip ( indices , clf_names ) : <EOL> plt . text ( - <NUM_LIT> , i , c ) <EOL> plt . show ( ) </s>
