<s> import numpy <EOL> from collections import OrderedDict <EOL> from hep_ml . grid_search import SimpleParameterOptimizer , AbstractParameterGenerator , GridOptimalSearchCV <EOL> from hep_ml . commonutils import generate_sample <EOL> __author__ = '<STR_LIT>' <EOL> numpy . random . seed ( <NUM_LIT> ) <EOL> class FunctionOptimizer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , function , param_grid , n_evaluations = <NUM_LIT:100> , parameter_generator_type = None ) : <EOL> """<STR_LIT>""" <EOL> self . function = function <EOL> if parameter_generator_type is None : <EOL> parameter_generator_type = SimpleParameterOptimizer <EOL> self . generator = parameter_generator_type ( param_grid , n_evaluations ) <EOL> def optimize ( self ) : <EOL> assert isinstance ( self . generator , AbstractParameterGenerator ) , '<STR_LIT>' '<STR_LIT>' <EOL> for _ in range ( self . generator . n_evaluations ) : <EOL> next_indices , next_params = self . generator . generate_next_point ( ) <EOL> value = self . function ( ** next_params ) <EOL> self . generator . add_result ( state_indices = next_indices , value = value ) <EOL> def print_results ( self , reorder = True ) : <EOL> self . generator . print_results ( reorder = reorder ) <EOL> def test_simple_optimizer ( n_evaluations = <NUM_LIT> ) : <EOL> optimizer = FunctionOptimizer ( lambda x , y , z , w : x * y * z * w , <EOL> param_grid = { '<STR_LIT:x>' : list ( range ( <NUM_LIT:11> ) ) , <EOL> '<STR_LIT:y>' : list ( range ( <NUM_LIT:11> ) ) , <EOL> '<STR_LIT:z>' : list ( range ( <NUM_LIT:11> ) ) , <EOL> '<STR_LIT:w>' : list ( range ( <NUM_LIT:11> ) ) <EOL> } , <EOL> n_evaluations = n_evaluations ) <EOL> optimizer . optimize ( ) <EOL> assert len ( optimizer . generator . grid_scores_ ) == n_evaluations <EOL> assert len ( optimizer . generator . queued_tasks_ ) == n_evaluations <EOL> assert set ( optimizer . generator . grid_scores_ . keys ( ) ) == optimizer . generator . queued_tasks_ <EOL> optimizer . print_results ( ) <EOL> def test_grid_search ( ) : <EOL> from sklearn . ensemble import AdaBoostClassifier <EOL> from sklearn . tree import DecisionTreeClassifier , ExtraTreeClassifier <EOL> grid = { '<STR_LIT>' : [ DecisionTreeClassifier ( max_depth = <NUM_LIT:3> ) , DecisionTreeClassifier ( max_depth = <NUM_LIT:4> ) , <EOL> ExtraTreeClassifier ( max_depth = <NUM_LIT:4> ) ] , <EOL> '<STR_LIT>' : [ <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT:0.5> , <NUM_LIT:1.> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT:5> , <NUM_LIT:10> , <NUM_LIT:15> , <NUM_LIT:20> , <NUM_LIT:30> , <NUM_LIT> , <NUM_LIT:50> , <NUM_LIT> , <NUM_LIT:100> , <NUM_LIT> ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] } <EOL> grid = OrderedDict ( grid ) <EOL> trainX , trainY = generate_sample ( <NUM_LIT> , <NUM_LIT:10> , distance = <NUM_LIT:0.5> ) <EOL> grid_cv = GridOptimalSearchCV ( AdaBoostClassifier ( ) , grid , n_evaluations = <NUM_LIT:10> , refit = True , log_name = '<STR_LIT:test>' ) <EOL> grid_cv . fit ( trainX , trainY ) <EOL> grid_cv . predict_proba ( trainX ) <EOL> grid_cv . predict ( trainX ) <EOL> grid_cv . print_param_stats ( [ <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT:0.5> , <NUM_LIT> ] ) </s>
