<s> from KNNLearner import KNNLearner <EOL> from LinRegLearner import LinRegLearner <EOL> import numpy <EOL> import numpy as np <EOL> import io , sys , time <EOL> import math <EOL> import matplotlib . pyplot as plt <EOL> class Timer : <EOL> def __enter__ ( self ) : <EOL> self . start = time . clock ( ) <EOL> return self <EOL> def __exit__ ( self , * args ) : <EOL> self . end = time . clock ( ) <EOL> self . interval = self . end - self . start <EOL> def get_graph ( x_series , y_series , xlabel , ylabel , name_file , ylim = None ) : <EOL> plt . clf ( ) <EOL> plt . xlabel ( xlabel ) <EOL> plt . ylabel ( ylabel ) <EOL> if ylim : <EOL> plt . ylim ( <NUM_LIT:0> , ylim ) <EOL> plt . plot ( x_series , y_series ) <EOL> plt . savefig ( name_file ) <EOL> def get_graph_two_plots ( x_series , y_series , y1_series , xlabel , ylabel , name_file ) : <EOL> plt . clf ( ) <EOL> plt . xlabel ( xlabel ) <EOL> plt . ylabel ( ylabel ) <EOL> first = plt . plot ( x_series , y_series ) <EOL> second = plt . plot ( x_series , y1_series ) <EOL> plt . legend ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> plt . savefig ( name_file ) <EOL> def scatter ( Y_return , Y_test , name_file ) : <EOL> plt . clf ( ) <EOL> fig = plt . figure ( figsize = ( <NUM_LIT:6> , <NUM_LIT:5> ) ) <EOL> graph = fig . add_subplot ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> graph . scatter ( Y_return , Y_test ) <EOL> graph . set_title ( "<STR_LIT>" ) <EOL> graph . set_xlabel = "<STR_LIT>" <EOL> graph . set_ylabel = "<STR_LIT:Y>" <EOL> fig . savefig ( name_file ) <EOL> def get_rmse ( Y_return , Y_Test ) : <EOL> return math . sqrt ( np . mean ( np . square ( Y_return - Y_Test ) ) ) <EOL> def get_correlation ( Y_return , Y_test ) : <EOL> covariance_matrix = np . corrcoef ( Y_return , np . squeeze ( np . asarray ( Y_test ) ) ) <EOL> return covariance_matrix [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> def linreglearner_test ( filenames ) : <EOL> for filename in filenames : <EOL> linreglearner = LinRegLearner ( ) <EOL> get_set = linreglearner . getflatcsv ( filename ) <EOL> get_set_60pr , get_set_40pr = numpy . split ( get_set , [ <NUM_LIT> ] ) <EOL> ( X , Y ) = numpy . split ( get_set , [ <NUM_LIT:2> ] , axis = <NUM_LIT:1> ) <EOL> ( XTrain , XTest ) = numpy . split ( X , [ <NUM_LIT> ] ) <EOL> ( Ytrain , YTest ) = numpy . split ( Y , [ <NUM_LIT> ] ) <EOL> with Timer ( ) as t : <EOL> linreglearner . addEvidence ( XTrain , Ytrain ) <EOL> print '<STR_LIT>' . format ( t . interval , filename ) <EOL> with Timer ( ) as t : <EOL> Y_return = linreglearner . query ( XTest ) <EOL> print '<STR_LIT>' . format ( t . interval , filename ) <EOL> def knnlearner_test ( filenames ) : <EOL> for filename in filenames : <EOL> train_time = [ ] <EOL> query_time = [ ] <EOL> rmse_series = [ ] <EOL> rmse_series_insample = [ ] <EOL> covariance_series = [ ] <EOL> for i in xrange ( <NUM_LIT:1> , <NUM_LIT> ) : <EOL> knnlearner = KNNLearner ( k = i ) <EOL> get_set = knnlearner . getflatcsv ( filename ) <EOL> get_set_60pr , get_set_40pr = numpy . split ( get_set , [ <NUM_LIT> ] ) <EOL> ( X , Y ) = numpy . split ( get_set , [ <NUM_LIT:2> ] , axis = <NUM_LIT:1> ) <EOL> ( XTrain , XTest ) = numpy . split ( X , [ <NUM_LIT> ] ) <EOL> ( Ytrain , YTest ) = numpy . split ( Y , [ <NUM_LIT> ] ) <EOL> knnlearner . build_hash ( get_set_60pr ) <EOL> with Timer ( ) as t : <EOL> knnlearner . addEvidence ( XTrain , Ytrain ) <EOL> train_time . append ( t . interval ) <EOL> query_X = numpy . array ( XTest ) <EOL> with Timer ( ) as t : <EOL> ( XY_return , Y_return ) = knnlearner . query ( XTest ) <EOL> query_time . append ( t . interval ) <EOL> Y_Test = np . squeeze ( np . asarray ( YTest ) ) <EOL> Y_Return = numpy . array ( Y_return ) <EOL> rmse_series . append ( get_rmse ( Y_Test , Y_Return ) ) <EOL> ( XY_return_insample , Y_return_insample ) = knnlearner . query ( XTrain ) <EOL> Y_Train = np . squeeze ( np . asarray ( Ytrain ) ) <EOL> Y_return_insample = numpy . array ( Y_return_insample ) <EOL> rmse_series_insample . append ( get_rmse ( Y_Train , Y_return_insample ) ) <EOL> covariance_series . append ( get_correlation ( Y_Test , Y_Return ) ) <EOL> min_rmse = min ( float ( i ) for i in rmse_series ) <EOL> k_index = rmse_series . index ( min_rmse ) <EOL> print "<STR_LIT>" , k_index + <NUM_LIT:1> , "<STR_LIT>" , filename <EOL> knnlearner_scatter = KNNLearner ( k = k_index + <NUM_LIT:1> ) <EOL> get_set = knnlearner_scatter . getflatcsv ( filename ) <EOL> get_set_60pr , get_set_40pr = numpy . split ( get_set , [ <NUM_LIT> ] ) <EOL> ( X , Y ) = numpy . split ( get_set , [ <NUM_LIT:2> ] , axis = <NUM_LIT:1> ) <EOL> ( XTrain , XTest ) = numpy . split ( X , [ <NUM_LIT> ] ) <EOL> ( Ytrain , YTest ) = numpy . split ( Y , [ <NUM_LIT> ] ) <EOL> knnlearner_scatter . build_hash ( get_set_60pr ) <EOL> knnlearner_scatter . addEvidence ( XTrain , Ytrain ) <EOL> ( XY_return , Y_return ) = knnlearner_scatter . query ( XTest ) <EOL> Y_Test = np . squeeze ( np . asarray ( YTest ) ) <EOL> Y_Return = numpy . array ( Y_return ) <EOL> scatter ( Y_Return , Y_Test , "<STR_LIT>" + filename + "<STR_LIT>" ) <EOL> get_graph ( numpy . arange ( <NUM_LIT:1> , <NUM_LIT> ) , train_time , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" + filename + "<STR_LIT>" , <NUM_LIT:4> ) <EOL> get_graph ( numpy . arange ( <NUM_LIT:1> , <NUM_LIT> ) , query_time , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" + filename + "<STR_LIT>" , <NUM_LIT:4> ) <EOL> get_graph ( numpy . arange ( <NUM_LIT:1> , <NUM_LIT> ) , rmse_series , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" + filename + "<STR_LIT>" ) <EOL> get_graph ( numpy . arange ( <NUM_LIT:1> , <NUM_LIT> ) , covariance_series , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" + filename + "<STR_LIT>" ) <EOL> get_graph_two_plots ( numpy . arange ( <NUM_LIT:1> , <NUM_LIT> ) , rmse_series_insample , rmse_series , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" + filename + "<STR_LIT>" ) <EOL> def main ( ) : <EOL> knnlearner_test ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> linreglearner_test ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
