<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> import matplotlib . pyplot as plt <EOL> from itertools import cycle <EOL> from sklearn . cluster import KMeans , SpectralClustering , AgglomerativeClustering <EOL> from sklearn . metrics import accuracy_score <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:2> ) <EOL> plt . show ( ) <EOL> def preprocess ( article_file , lable_file , k ) : <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 = k ) <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> nFeatures = np . arange ( <NUM_LIT:10> , <NUM_LIT:100> , <NUM_LIT:10> ) <EOL> data = { } <EOL> for k in nFeatures : <EOL> features , labels , vectorizer , selector , le , features_data = preprocess ( "<STR_LIT>" , "<STR_LIT>" , k ) <EOL> for name , clf in [ <EOL> ( '<STR_LIT>' , KMeans ( n_clusters = <NUM_LIT:2> , n_init = <NUM_LIT:5> ) ) , <EOL> ( '<STR_LIT>' , SpectralClustering ( n_clusters = <NUM_LIT:2> , n_init = <NUM_LIT:5> ) ) , <EOL> ( '<STR_LIT>' , AgglomerativeClustering ( n_clusters = <NUM_LIT:2> , linkage = '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , AgglomerativeClustering ( n_clusters = <NUM_LIT:2> , linkage = '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , AgglomerativeClustering ( n_clusters = <NUM_LIT:2> , linkage = '<STR_LIT>' ) ) <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> y_pred = clf . fit ( features ) . labels_ <EOL> print "<STR_LIT>" , round ( time ( ) - t0 , <NUM_LIT:3> ) , "<STR_LIT:s>" <EOL> score_accuracy = accuracy_score ( y_pred , labels , normalize = True ) <EOL> print ( '<STR_LIT>' . format ( score_accuracy ) ) <EOL> print "<STR_LIT:*>" * <NUM_LIT:100> <EOL> data [ name ] . append ( score_accuracy ) <EOL> plot ( nFeatures , data ) </s>
