<s> """<STR_LIT>""" <EOL> from time import time <EOL> import numpy as np <EOL> import pylab as pl <EOL> from matplotlib import ticker <EOL> from sklearn import neighbors , datasets <EOL> def get_data ( N , D , dataset = '<STR_LIT>' ) : <EOL> if dataset == '<STR_LIT>' : <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> return np . random . random ( ( N , D ) ) <EOL> elif dataset == '<STR_LIT>' : <EOL> X = datasets . load_digits ( ) . data <EOL> i = np . argsort ( X [ <NUM_LIT:0> ] ) [ : : - <NUM_LIT:1> ] <EOL> X = X [ : , i ] <EOL> return X [ : N , : D ] <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % dataset ) <EOL> def barplot_neighbors ( Nrange = <NUM_LIT:2> ** np . arange ( <NUM_LIT:1> , <NUM_LIT:11> ) , <EOL> Drange = <NUM_LIT:2> ** np . arange ( <NUM_LIT:7> ) , <EOL> krange = <NUM_LIT:2> ** np . arange ( <NUM_LIT:10> ) , <EOL> N = <NUM_LIT:1000> , <EOL> D = <NUM_LIT:64> , <EOL> k = <NUM_LIT:5> , <EOL> leaf_size = <NUM_LIT:30> , <EOL> dataset = '<STR_LIT>' ) : <EOL> algorithms = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> fiducial_values = { '<STR_LIT:N>' : N , <EOL> '<STR_LIT:D>' : D , <EOL> '<STR_LIT:k>' : k } <EOL> N_results_build = dict ( [ ( alg , np . zeros ( len ( Nrange ) ) ) <EOL> for alg in algorithms ] ) <EOL> N_results_query = dict ( [ ( alg , np . zeros ( len ( Nrange ) ) ) <EOL> for alg in algorithms ] ) <EOL> for i , NN in enumerate ( Nrange ) : <EOL> print ( "<STR_LIT>" % ( NN , i + <NUM_LIT:1> , len ( Nrange ) ) ) <EOL> X = get_data ( NN , D , dataset ) <EOL> for algorithm in algorithms : <EOL> nbrs = neighbors . NearestNeighbors ( n_neighbors = min ( NN , k ) , <EOL> algorithm = algorithm , <EOL> leaf_size = leaf_size ) <EOL> t0 = time ( ) <EOL> nbrs . fit ( X ) <EOL> t1 = time ( ) <EOL> nbrs . kneighbors ( X ) <EOL> t2 = time ( ) <EOL> N_results_build [ algorithm ] [ i ] = ( t1 - t0 ) <EOL> N_results_query [ algorithm ] [ i ] = ( t2 - t1 ) <EOL> D_results_build = dict ( [ ( alg , np . zeros ( len ( Drange ) ) ) <EOL> for alg in algorithms ] ) <EOL> D_results_query = dict ( [ ( alg , np . zeros ( len ( Drange ) ) ) <EOL> for alg in algorithms ] ) <EOL> for i , DD in enumerate ( Drange ) : <EOL> print ( "<STR_LIT>" % ( DD , i + <NUM_LIT:1> , len ( Drange ) ) ) <EOL> X = get_data ( N , DD , dataset ) <EOL> for algorithm in algorithms : <EOL> nbrs = neighbors . NearestNeighbors ( n_neighbors = k , <EOL> algorithm = algorithm , <EOL> leaf_size = leaf_size ) <EOL> t0 = time ( ) <EOL> nbrs . fit ( X ) <EOL> t1 = time ( ) <EOL> nbrs . kneighbors ( X ) <EOL> t2 = time ( ) <EOL> D_results_build [ algorithm ] [ i ] = ( t1 - t0 ) <EOL> D_results_query [ algorithm ] [ i ] = ( t2 - t1 ) <EOL> k_results_build = dict ( [ ( alg , np . zeros ( len ( krange ) ) ) <EOL> for alg in algorithms ] ) <EOL> k_results_query = dict ( [ ( alg , np . zeros ( len ( krange ) ) ) <EOL> for alg in algorithms ] ) <EOL> X = get_data ( N , DD , dataset ) <EOL> for i , kk in enumerate ( krange ) : <EOL> print ( "<STR_LIT>" % ( kk , i + <NUM_LIT:1> , len ( krange ) ) ) <EOL> for algorithm in algorithms : <EOL> nbrs = neighbors . NearestNeighbors ( n_neighbors = kk , <EOL> algorithm = algorithm , <EOL> leaf_size = leaf_size ) <EOL> t0 = time ( ) <EOL> nbrs . fit ( X ) <EOL> t1 = time ( ) <EOL> nbrs . kneighbors ( X ) <EOL> t2 = time ( ) <EOL> k_results_build [ algorithm ] [ i ] = ( t1 - t0 ) <EOL> k_results_query [ algorithm ] [ i ] = ( t2 - t1 ) <EOL> pl . figure ( figsize = ( <NUM_LIT:8> , <NUM_LIT:11> ) ) <EOL> for ( sbplt , vals , quantity , <EOL> build_time , query_time ) in [ ( <NUM_LIT> , Nrange , '<STR_LIT:N>' , <EOL> N_results_build , <EOL> N_results_query ) , <EOL> ( <NUM_LIT> , Drange , '<STR_LIT:D>' , <EOL> D_results_build , <EOL> D_results_query ) , <EOL> ( <NUM_LIT> , krange , '<STR_LIT:k>' , <EOL> k_results_build , <EOL> k_results_query ) ] : <EOL> ax = pl . subplot ( sbplt , yscale = '<STR_LIT>' ) <EOL> pl . grid ( True ) <EOL> tick_vals = [ ] <EOL> tick_labels = [ ] <EOL> bottom = <NUM_LIT:10> ** np . min ( [ min ( np . floor ( np . log10 ( build_time [ alg ] ) ) ) <EOL> for alg in algorithms ] ) <EOL> for i , alg in enumerate ( algorithms ) : <EOL> xvals = <NUM_LIT:0.1> + i * ( <NUM_LIT:1> + len ( vals ) ) + np . arange ( len ( vals ) ) <EOL> width = <NUM_LIT> <EOL> c_bar = pl . bar ( xvals , build_time [ alg ] - bottom , <EOL> width , bottom , color = '<STR_LIT:r>' ) <EOL> q_bar = pl . bar ( xvals , query_time [ alg ] , <EOL> width , build_time [ alg ] , color = '<STR_LIT:b>' ) <EOL> tick_vals += list ( xvals + <NUM_LIT:0.5> * width ) <EOL> tick_labels += [ '<STR_LIT>' % val for val in vals ] <EOL> pl . text ( ( i + <NUM_LIT> ) / len ( algorithms ) , <NUM_LIT> , alg , <EOL> transform = ax . transAxes , <EOL> ha = '<STR_LIT:left>' , <EOL> va = '<STR_LIT>' , <EOL> bbox = dict ( facecolor = '<STR_LIT:w>' , edgecolor = '<STR_LIT:w>' , alpha = <NUM_LIT:0.5> ) ) <EOL> pl . ylabel ( '<STR_LIT>' ) <EOL> ax . xaxis . set_major_locator ( ticker . FixedLocator ( tick_vals ) ) <EOL> ax . xaxis . set_major_formatter ( ticker . FixedFormatter ( tick_labels ) ) <EOL> for label in ax . get_xticklabels ( ) : <EOL> label . set_rotation ( - <NUM_LIT> ) <EOL> label . set_fontsize ( <NUM_LIT:10> ) <EOL> title_string = '<STR_LIT>' % quantity <EOL> descr_string = '<STR_LIT>' <EOL> for s in '<STR_LIT>' : <EOL> if s == quantity : <EOL> pass <EOL> else : <EOL> descr_string += '<STR_LIT>' % ( s , fiducial_values [ s ] ) <EOL> descr_string = descr_string [ : - <NUM_LIT:2> ] <EOL> pl . text ( <NUM_LIT> , <NUM_LIT:0.5> , title_string , <EOL> transform = ax . transAxes , rotation = - <NUM_LIT> , <EOL> ha = '<STR_LIT:left>' , va = '<STR_LIT>' , fontsize = <NUM_LIT:20> ) <EOL> pl . text ( <NUM_LIT> , <NUM_LIT:0.5> , descr_string , <EOL> transform = ax . transAxes , rotation = - <NUM_LIT> , <EOL> ha = '<STR_LIT:right>' , va = '<STR_LIT>' ) <EOL> pl . gcf ( ) . suptitle ( "<STR_LIT>" % dataset . capitalize ( ) , fontsize = <NUM_LIT:16> ) <EOL> pl . figlegend ( ( c_bar , q_bar ) , ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> barplot_neighbors ( dataset = '<STR_LIT>' ) <EOL> barplot_neighbors ( dataset = '<STR_LIT>' ) <EOL> pl . show ( ) </s>
