-
Notifications
You must be signed in to change notification settings - Fork 106
/
experiment_over_parameters.py
97 lines (80 loc) · 2.71 KB
/
experiment_over_parameters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# The following two utility functions are useful when you want
# to try out a variety of argument values for a given function.
import graphlab as gl
import itertools
def crossproduct(d):
"""
Create an SFrame containing the crossproduct of all provided options.
Parameters
----------
d : dict
Each key is the name of an option, and each value is a list
of the possible values for that option.
Returns
-------
out : SFrame
There will be a column for each key in the provided dictionary,
and a row for each unique combination of all values.
Example
-------
settings = {'argument_1':[0, 1],
'argument_2':['a', 'b', 'c']}
print crossproduct(settings)
+------------+------------+
| argument_2 | argument_1 |
+------------+------------+
| a | 0 |
| a | 1 |
| b | 0 |
| b | 1 |
| c | 0 |
| c | 1 |
+------------+------------+
[6 rows x 2 columns]
"""
d = [zip(d.keys(), x)
for x in itertools.product(*d.values())]
sa = [{k:v for (k,v) in x} for x in d]
return gl.SArray(sa).unpack(column_name_prefix='')
def map_across_rows(my_function, argument_sf):
"""
Run a function using the arguments provided by each row in the SFrame.
The function is assumed to return a dict.
Example
-------
def f(argument_1, argument_2):
return {'my_metric': argument_1 * argument_2}
settings = {'argument_1':[0, 1],
'argument_2':[0, 5, 10]}
sf = crossproduct(settings)
sf['results'] = map_across_rows(f, sf)
Columns:
argument_2 int
argument_1 int
results dict
Rows: 6
Data:
+------------+------------+-------------------+
| argument_2 | argument_1 | results |
+------------+------------+-------------------+
| 0 | 0 | {'my_metric': 0} |
| 0 | 1 | {'my_metric': 0} |
| 5 | 0 | {'my_metric': 0} |
| 5 | 1 | {'my_metric': 5} |
| 10 | 0 | {'my_metric': 0} |
| 10 | 1 | {'my_metric': 10} |
+------------+------------+-------------------+
[6 rows x 3 columns]
"""
my_settings = argument_sf.pack_columns(dtype=dict)['X1']
res = [my_function(**args) for args in my_settings]
return gl.SArray(res)
# Examples
def f(argument_1, argument_2):
return {'my_metric': argument_1 * argument_2}
settings = {'argument_1':[0, 1],
'argument_2':[0, 5, 10]}
sf = crossproduct(settings)
print sf
sf['results'] = map_across_rows(f, sf)
print sf