#!/usr/bin/env python from datamind.ml.explorer import GridExplorer # define some functions to be called for each step of grid exploring def printer1(indexes, params, user_data): ''' Print a dictionary representing current parameter values. param : dictionary of parameters current values. user_data : ignore this parameter in this example.''' print "\t", params def printer2(indexes, (names, values), user_data): ''' Print a list of current parameter values and fetch parameter values thanks to user data. param : list of current value for each parameter. Here the name of each parameter was lost for computation efficiency. user_data : user additional data (dictionary-shaped). Here we add a reference to a list of parameter names.''' print '\t', names, values def count(indexes, params, user_data): ''' increment counter of user data for each element of grid exploring.''' user_data['count'] += 1 for k, v in params.items(): user_data['sum'][k] += v # Examples def example1(g) : ''' Simple example showing how to use grid exploring to display values of parameters at each step.''' # Explore : call printer function for each possible parameters of # cartesian product of parameters sets. print "*** printer - dictionary shape ***" g.explore(printer1) print "*** printer - tuple of list shape ***" g.explore(printer2, as_dict=False) def example2(g): ''' Example of one user_data use to count number of data covered during exploration.''' # additional data to pass to user function data = {'count' : 0, 'sum' : {'x' : 0, 'y' : 0, 'z' : 0}} print "*** counter ***" g.explore(count, data) print '\tnumber of data :', data['count'] mean = data['sum'] for k, v in mean.items(): mean[k] = v / float(data['count']) print '\tmean of covered space data :', mean # Main function : declare gridExplorer instance def main(): # value ranges for 3 parameters (x, y, z) ranges = {'x' : [8, 9], 'y' : [10, 20, 30], 'z' : [44, 55, 66, 77]} # Create a grid explorer g = GridExplorer(ranges) # examples based on g instance example1(g) example2(g) if __name__ == "__main__" : main()