<s> from sklearn . metrics import accuracy_score <EOL> from time import time <EOL> import numpy as np <EOL> import pickle <EOL> from sklearn . feature_extraction . text import TfidfVectorizer <EOL> from sklearn import preprocessing <EOL> from sklearn . feature_selection import SelectPercentile , f_classif , chi2 <EOL> from sklearn . feature_selection import SelectKBest , f_regression <EOL> from sklearn . ensemble import AdaBoostClassifier <EOL> from sklearn . naive_bayes import BernoulliNB <EOL> from sklearn . naive_bayes import GaussianNB <EOL> from sklearn . tree import DecisionTreeClassifier <EOL> from sklearn . neighbors import KNeighborsClassifier <EOL> from sklearn . ensemble import RandomForestClassifier <EOL> from sklearn . svm import SVC <EOL> from sklearn import cross_validation <EOL> from sklearn . decomposition import RandomizedPCA <EOL> import matplotlib . pyplot as plt <EOL> from itertools import cycle <EOL> np . set_printoptions ( <NUM_LIT:4> , suppress = True ) <EOL> def plot ( nFeatures , data ) : <EOL> colors = cycle ( '<STR_LIT>' ) <EOL> algorithm = sorted ( data ) <EOL> fig = plt . figure ( ) <EOL> ax = fig . add_subplot ( <NUM_LIT> ) <EOL> for j , c in zip ( algorithm , colors ) : <EOL> ax . plot ( nFeatures , data [ j ] , label = j , color = c ) <EOL> ax . scatter ( nFeatures , data [ j ] , color = c ) <EOL> plt . xlabel ( "<STR_LIT>" ) <EOL> plt . ylabel ( "<STR_LIT>" ) <EOL> plt . title ( "<STR_LIT>" ) <EOL> box = ax . get_position ( ) <EOL> ax . set_position ( [ box . x0 , box . y0 + box . height * <NUM_LIT> , <EOL> box . width , box . height * <NUM_LIT> ] ) <EOL> ax . legend ( loc = "<STR_LIT>" , bbox_to_anchor = ( <NUM_LIT:0.5> , - <NUM_LIT> ) , fancybox = True , shadow = True , ncol = <NUM_LIT:3> ) <EOL> plt . legend ( loc = <NUM_LIT:4> ) <EOL> plt . show ( ) <EOL> def preprocess ( article_file , lable_file ) : <EOL> features = pickle . load ( open ( article_file ) ) <EOL> features = np . array ( features ) <EOL> lables = pickle . load ( open ( lable_file ) ) <EOL> le = preprocessing . LabelEncoder ( ) <EOL> le . fit ( lables ) <EOL> lables = le . transform ( lables ) <EOL> vectorizer = TfidfVectorizer ( sublinear_tf = True , max_df = <NUM_LIT:0.5> , min_df = <NUM_LIT:1> , <EOL> stop_words = '<STR_LIT>' ) <EOL> features_train_transformed = vectorizer . fit_transform ( features ) <EOL> selector = SelectPercentile ( f_classif , percentile = <NUM_LIT> ) <EOL> selector . fit ( features_train_transformed , lables ) <EOL> features_train_transformed = selector . transform ( features_train_transformed ) . toarray ( ) <EOL> return features_train_transformed , lables , vectorizer , selector , le , features <EOL> nPCA = np . arange ( <NUM_LIT:20> , <NUM_LIT:200> , <NUM_LIT:20> ) <EOL> data = { } <EOL> for k in nPCA : <EOL> features , labels , vectorizer , selector , le , features_data = preprocess ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> features_train , features_test , labels_train , labels_test = cross_validation . train_test_split ( features , labels , test_size = <NUM_LIT:0.1> , random_state = <NUM_LIT> ) <EOL> t0 = time ( ) <EOL> pca = RandomizedPCA ( n_components = k ) <EOL> pca . fit ( features_train ) <EOL> print ( "<STR_LIT>" , round ( time ( ) - t0 , <NUM_LIT:3> ) , "<STR_LIT:s>" ) <EOL> features_train = pca . transform ( features_train ) <EOL> features_test = pca . transform ( features_test ) <EOL> for name , clf in [ <EOL> ( '<STR_LIT>' , AdaBoostClassifier ( algorithm = '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , BernoulliNB ( alpha = <NUM_LIT:1> ) ) , <EOL> ( '<STR_LIT>' , GaussianNB ( ) ) , <EOL> ( '<STR_LIT>' , DecisionTreeClassifier ( min_samples_split = <NUM_LIT:100> ) ) , <EOL> ( '<STR_LIT>' , KNeighborsClassifier ( n_neighbors = <NUM_LIT:50> , algorithm = '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , RandomForestClassifier ( min_samples_split = <NUM_LIT:100> ) ) , <EOL> ( '<STR_LIT>' , SVC ( kernel = '<STR_LIT>' , C = <NUM_LIT:1> ) ) <EOL> ] : <EOL> if not data . has_key ( name ) : <EOL> data [ name ] = [ ] <EOL> print "<STR_LIT:*>" * <NUM_LIT:100> <EOL> print ( '<STR_LIT>' . format ( name ) + '<STR_LIT>' . format ( k ) ) <EOL> t0 = time ( ) <EOL> clf . fit ( features_train , labels_train ) <EOL> print ( "<STR_LIT>" , round ( time ( ) - t0 , <NUM_LIT:3> ) , "<STR_LIT:s>" ) <EOL> y_pred = clf . predict ( features_test ) <EOL> print ( "<STR_LIT>" , round ( time ( ) - t0 , <NUM_LIT:3> ) , "<STR_LIT:s>" ) <EOL> score_accuracy = accuracy_score ( y_pred , labels_test , normalize = True ) <EOL> print ( '<STR_LIT>' . format ( score_accuracy ) ) <EOL> print "<STR_LIT:*>" * <NUM_LIT:100> <EOL> data [ name ] . append ( score_accuracy ) <EOL> plot ( nPCA , data ) </s>
