<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> from __future__ import division <EOL> from __future__ import absolute_import <EOL> from __future__ import print_function <EOL> from . mltools import * <EOL> import numpy as np <EOL> import optunity <EOL> import ast <EOL> import sys <EOL> if sys . version_info < ( <NUM_LIT:3> , <NUM_LIT:0> ) : <EOL> import ConfigParser as configparser <EOL> else : <EOL> import configparser <EOL> from pkg_resources import Requirement , resource_filename <EOL> _ELMK_CONFIG = resource_filename ( Requirement . parse ( "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> class ELMKernel ( MLTools ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , params = [ ] ) : <EOL> """<STR_LIT>""" <EOL> super ( self . __class__ , self ) . __init__ ( ) <EOL> self . regressor_name = "<STR_LIT>" <EOL> self . available_kernel_functions = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> self . default_param_kernel_function = "<STR_LIT>" <EOL> self . default_param_c = <NUM_LIT:9> <EOL> self . default_param_kernel_params = [ - <NUM_LIT:15> ] <EOL> self . output_weight = [ ] <EOL> self . training_patterns = [ ] <EOL> if not params : <EOL> self . param_kernel_function = self . default_param_kernel_function <EOL> self . param_c = self . default_param_c <EOL> self . param_kernel_params = self . default_param_kernel_params <EOL> else : <EOL> self . param_kernel_function = params [ <NUM_LIT:0> ] <EOL> self . param_c = params [ <NUM_LIT:1> ] <EOL> self . param_kernel_params = params [ <NUM_LIT:2> ] <EOL> def _kernel_matrix ( self , training_patterns , kernel_type , kernel_param , <EOL> test_patterns = None ) : <EOL> """<STR_LIT>""" <EOL> number_training_patterns = training_patterns . shape [ <NUM_LIT:0> ] <EOL> if kernel_type == "<STR_LIT>" : <EOL> if test_patterns is None : <EOL> temp_omega = np . dot ( <EOL> np . sum ( training_patterns ** <NUM_LIT:2> , axis = <NUM_LIT:1> ) . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) , <EOL> np . ones ( ( <NUM_LIT:1> , number_training_patterns ) ) ) <EOL> temp_omega = temp_omega + temp_omega . conj ( ) . T <EOL> omega = np . exp ( <EOL> - ( <NUM_LIT:2> ** kernel_param [ <NUM_LIT:0> ] ) * ( temp_omega - <NUM_LIT:2> * ( np . dot ( <EOL> training_patterns , training_patterns . conj ( ) . T ) ) ) ) <EOL> else : <EOL> number_test_patterns = test_patterns . shape [ <NUM_LIT:0> ] <EOL> temp1 = np . dot ( <EOL> np . sum ( training_patterns ** <NUM_LIT:2> , axis = <NUM_LIT:1> ) . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) , <EOL> np . ones ( ( <NUM_LIT:1> , number_test_patterns ) ) ) <EOL> temp2 = np . dot ( <EOL> np . sum ( test_patterns ** <NUM_LIT:2> , axis = <NUM_LIT:1> ) . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) , <EOL> np . ones ( ( <NUM_LIT:1> , number_training_patterns ) ) ) <EOL> temp_omega = temp1 + temp2 . conj ( ) . T <EOL> omega = np . exp ( - ( <NUM_LIT:2> ** kernel_param [ <NUM_LIT:0> ] ) * <EOL> ( temp_omega - <NUM_LIT:2> * np . dot ( training_patterns , <EOL> test_patterns . conj ( ) . T ) ) ) <EOL> elif kernel_type == "<STR_LIT>" : <EOL> if test_patterns is None : <EOL> omega = np . dot ( training_patterns , training_patterns . conj ( ) . T ) <EOL> else : <EOL> omega = np . dot ( training_patterns , test_patterns . conj ( ) . T ) <EOL> elif kernel_type == "<STR_LIT>" : <EOL> kernel_param [ <NUM_LIT:1> ] = round ( kernel_param [ <NUM_LIT:1> ] ) <EOL> if test_patterns is None : <EOL> temp = np . dot ( training_patterns , training_patterns . conj ( ) . T ) + kernel_param [ <NUM_LIT:0> ] <EOL> omega = temp ** kernel_param [ <NUM_LIT:1> ] <EOL> else : <EOL> temp = np . dot ( training_patterns , test_patterns . conj ( ) . T ) + kernel_param [ <NUM_LIT:0> ] <EOL> omega = temp ** kernel_param [ <NUM_LIT:1> ] <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> return omega <EOL> def _local_train ( self , training_patterns , training_expected_targets , <EOL> params ) : <EOL> if not params : <EOL> pass <EOL> else : <EOL> self . param_kernel_function = params [ <NUM_LIT:0> ] <EOL> self . param_c = params [ <NUM_LIT:1> ] <EOL> self . param_kernel_params = params [ <NUM_LIT:2> ] <EOL> self . training_patterns = training_patterns <EOL> number_training_patterns = self . training_patterns . shape [ <NUM_LIT:0> ] <EOL> omega_train = self . _kernel_matrix ( self . training_patterns , <EOL> self . param_kernel_function , <EOL> self . param_kernel_params ) <EOL> self . output_weight = np . linalg . solve ( <EOL> ( omega_train + np . eye ( number_training_patterns ) / <EOL> ( <NUM_LIT:2> ** self . param_c ) ) , <EOL> training_expected_targets ) . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> training_predicted_targets = np . dot ( omega_train , self . output_weight ) <EOL> return training_predicted_targets <EOL> def _local_test ( self , testing_patterns , testing_expected_targets , <EOL> predicting ) : <EOL> omega_test = self . _kernel_matrix ( self . training_patterns , <EOL> self . param_kernel_function , <EOL> self . param_kernel_params , <EOL> testing_patterns ) <EOL> testing_predicted_targets = np . dot ( omega_test . conj ( ) . T , <EOL> self . output_weight ) <EOL> return testing_predicted_targets <EOL> def get_available_kernel_functions ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . available_kernel_functions <EOL> def print_parameters ( self ) : <EOL> """<STR_LIT>""" <EOL> print ( ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( ) <EOL> print ( "<STR_LIT>" , self . param_c ) <EOL> print ( "<STR_LIT>" , self . param_kernel_function ) <EOL> print ( "<STR_LIT>" , self . param_kernel_params ) <EOL> print ( ) <EOL> print ( "<STR_LIT>" , self . cv_best_rmse ) <EOL> print ( ) <EOL> def search_param ( self , database , dataprocess = None , path_filename = ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> save = False , cv = "<STR_LIT>" , of = "<STR_LIT>" , kf = None , eval = <NUM_LIT:50> ) : <EOL> """<STR_LIT>""" <EOL> if kf is None : <EOL> search_kernel_functions = self . available_kernel_functions <EOL> elif type ( kf ) is list : <EOL> search_kernel_functions = kf <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> print ( self . regressor_name ) <EOL> print ( "<STR_LIT>" ) <EOL> config = configparser . ConfigParser ( ) <EOL> if sys . version_info < ( <NUM_LIT:3> , <NUM_LIT:0> ) : <EOL> config . readfp ( open ( _ELMK_CONFIG ) ) <EOL> else : <EOL> config . read_file ( open ( _ELMK_CONFIG ) ) <EOL> best_function_error = <NUM_LIT> <EOL> temp_error = best_function_error <EOL> best_param_c = <NUM_LIT:0> <EOL> best_param_kernel_function = "<STR_LIT>" <EOL> best_param_kernel_param = [ ] <EOL> for kernel_function in search_kernel_functions : <EOL> if sys . version_info < ( <NUM_LIT:3> , <NUM_LIT:0> ) : <EOL> elmk_c_range = ast . literal_eval ( config . get ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) ) <EOL> n_parameters = config . getint ( kernel_function , "<STR_LIT>" ) <EOL> kernel_p_range = ast . literal_eval ( config . get ( kernel_function , <EOL> "<STR_LIT>" ) ) <EOL> else : <EOL> kernel_config = config [ kernel_function ] <EOL> elmk_c_range = ast . literal_eval ( kernel_config [ "<STR_LIT>" ] ) <EOL> n_parameters = int ( kernel_config [ "<STR_LIT>" ] ) <EOL> kernel_p_range = ast . literal_eval ( kernel_config [ "<STR_LIT>" ] ) <EOL> param_ranges = [ [ elmk_c_range [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , elmk_c_range [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ] ] <EOL> for param in range ( n_parameters ) : <EOL> param_ranges . append ( [ kernel_p_range [ param ] [ <NUM_LIT:0> ] , <EOL> kernel_p_range [ param ] [ <NUM_LIT:1> ] ] ) <EOL> def wrapper_0param ( param_c ) : <EOL> """<STR_LIT>""" <EOL> if cv == "<STR_LIT>" : <EOL> cv_tr_error , cv_te_error = time_series_cross_validation ( self , database , <EOL> [ kernel_function , <EOL> param_c , <EOL> list ( [ ] ) ] , <EOL> number_folds = <NUM_LIT:10> , <EOL> dataprocess = dataprocess ) <EOL> elif cv == "<STR_LIT>" : <EOL> cv_tr_error , cv_te_error = kfold_cross_validation ( self , database , <EOL> [ kernel_function , <EOL> param_c , <EOL> list ( [ ] ) ] , <EOL> number_folds = <NUM_LIT:10> , <EOL> dataprocess = dataprocess ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if of == "<STR_LIT>" : <EOL> util = <NUM_LIT:1> / cv_te_error . get_accuracy ( ) <EOL> else : <EOL> util = cv_te_error . get ( of ) <EOL> return util <EOL> def wrapper_1param ( param_c , param_kernel ) : <EOL> """<STR_LIT>""" <EOL> if cv == "<STR_LIT>" : <EOL> cv_tr_error , cv_te_error = time_series_cross_validation ( self , database , <EOL> [ kernel_function , <EOL> param_c , <EOL> list ( [ param_kernel ] ) ] , <EOL> number_folds = <NUM_LIT:10> , <EOL> dataprocess = dataprocess ) <EOL> elif cv == "<STR_LIT>" : <EOL> cv_tr_error , cv_te_error = kfold_cross_validation ( self , database , <EOL> [ kernel_function , <EOL> param_c , <EOL> list ( [ param_kernel ] ) ] , <EOL> number_folds = <NUM_LIT:10> , <EOL> dataprocess = dataprocess ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if of == "<STR_LIT>" : <EOL> util = <NUM_LIT:1> / cv_te_error . get_accuracy ( ) <EOL> else : <EOL> util = cv_te_error . get ( of ) <EOL> return util <EOL> def wrapper_2param ( param_c , param_kernel1 , param_kernel2 ) : <EOL> """<STR_LIT>""" <EOL> if cv == "<STR_LIT>" : <EOL> cv_tr_error , cv_te_error = time_series_cross_validation ( self , database , <EOL> [ kernel_function , <EOL> param_c , <EOL> list ( [ param_kernel1 , <EOL> param_kernel2 ] ) ] , <EOL> number_folds = <NUM_LIT:10> , <EOL> dataprocess = dataprocess ) <EOL> elif cv == "<STR_LIT>" : <EOL> cv_tr_error , cv_te_error = kfold_cross_validation ( self , database , <EOL> [ kernel_function , <EOL> param_c , <EOL> list ( [ param_kernel1 , <EOL> param_kernel2 ] ) ] , <EOL> number_folds = <NUM_LIT:10> , <EOL> dataprocess = dataprocess ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if of == "<STR_LIT>" : <EOL> util = <NUM_LIT:1> / cv_te_error . get_accuracy ( ) <EOL> else : <EOL> util = cv_te_error . get ( of ) <EOL> return util <EOL> if kernel_function == "<STR_LIT>" : <EOL> optimal_parameters , details , _ = optunity . minimize ( wrapper_0param , <EOL> solver_name = "<STR_LIT>" , <EOL> num_evals = eval , <EOL> param_c = param_ranges [ <NUM_LIT:0> ] ) <EOL> elif kernel_function == "<STR_LIT>" : <EOL> optimal_parameters , details , _ = optunity . minimize ( wrapper_1param , <EOL> solver_name = "<STR_LIT>" , <EOL> num_evals = eval , <EOL> param_c = param_ranges [ <NUM_LIT:0> ] , <EOL> param_kernel = param_ranges [ <NUM_LIT:1> ] ) <EOL> elif kernel_function == "<STR_LIT>" : <EOL> optimal_parameters , details , _ = optunity . minimize ( wrapper_2param , <EOL> solver_name = "<STR_LIT>" , <EOL> num_evals = eval , <EOL> param_c = param_ranges [ <NUM_LIT:0> ] , <EOL> param_kernel1 = param_ranges [ <NUM_LIT:1> ] , <EOL> param_kernel2 = param_ranges [ <NUM_LIT:2> ] ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if details [ <NUM_LIT:0> ] < temp_error : <EOL> temp_error = details [ <NUM_LIT:0> ] <EOL> if of == "<STR_LIT>" : <EOL> best_function_error = <NUM_LIT:1> / temp_error <EOL> else : <EOL> best_function_error = temp_error <EOL> best_param_kernel_function = kernel_function <EOL> best_param_c = optimal_parameters [ "<STR_LIT>" ] <EOL> if best_param_kernel_function == "<STR_LIT>" : <EOL> best_param_kernel_param = [ ] <EOL> elif best_param_kernel_function == "<STR_LIT>" : <EOL> best_param_kernel_param = [ optimal_parameters [ "<STR_LIT>" ] ] <EOL> elif best_param_kernel_function == "<STR_LIT>" : <EOL> best_param_kernel_param = [ optimal_parameters [ "<STR_LIT>" ] , <EOL> optimal_parameters [ "<STR_LIT>" ] ] <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if of == "<STR_LIT>" : <EOL> print ( "<STR_LIT>" , kernel_function , <EOL> "<STR_LIT>" , <NUM_LIT:1> / details [ <NUM_LIT:0> ] ) <EOL> else : <EOL> print ( "<STR_LIT>" , kernel_function , <EOL> "<STR_LIT>" , details [ <NUM_LIT:0> ] ) <EOL> self . cv_best_rmse = best_function_error <EOL> self . param_c = best_param_c <EOL> self . param_kernel_function = best_param_kernel_function <EOL> self . param_kernel_params = best_param_kernel_param <EOL> print ( "<STR_LIT>" ) <EOL> self . print_parameters ( ) <EOL> return None <EOL> def train ( self , training_matrix , params = [ ] ) : <EOL> """<STR_LIT>""" <EOL> return self . _ml_train ( training_matrix , params ) <EOL> def test ( self , testing_matrix , predicting = False ) : <EOL> """<STR_LIT>""" <EOL> return self . _ml_test ( testing_matrix , predicting ) <EOL> @ copy_doc_of ( MLTools . _ml_predict ) <EOL> def predict ( self , horizon = <NUM_LIT:1> ) : <EOL> return self . _ml_predict ( horizon ) <EOL> @ copy_doc_of ( MLTools . _ml_train_iterative ) <EOL> def train_iterative ( self , database_matrix , params = [ ] , <EOL> sliding_window = <NUM_LIT> , k = <NUM_LIT:1> ) : <EOL> return self . _ml_train_iterative ( database_matrix , params , <EOL> sliding_window , k ) </s>
