Looping over Randomized Parameters
In the Partial randomization section, we explained how to generate graphs from a graph parameter space. What might come after, is running an algorithm on many generated graphs and measuring the performance of the algorithm with respect to a graph parameter, e.g. the number of nodes. GuidelineIterator
allows us to change a graph parameter in a loop, and do such analyses.
To use the iterator, the guideline outline doesn’t need to change. The only point is that the randomization directive of the target parameter, becomes the looping directive. Let’s say we want to iterate over number of nodes and run an algorithm:
1from pyparcs import RandomDescription, Graph, GuidelineIterator
2import numpy
3numpy.random.seed(42)
4
5iterator = GuidelineIterator(
6 {'nodes': {'bernoulli': {'p_': [['f-range', -1, 1], ['f-range', -5, -3, 3, 5], 0]},
7 'poisson': {'lambda_': [['f-range', 2, 4], ['f-range', 3, 5], 0]}},
8 'edges': {'identity': None},
9 'graph': {'density': ['f-range', 0.4, 1], 'num_nodes': ['i-range', 2, 8]}}
10)
11
12for guideline in iterator.get_guidelines(route='graph.num_nodes', steps=2):
13 description = RandomDescription(guideline, node_prefix='X')
14 graph = Graph(description)
15 samples, _ = graph.sample(4)
16 print(samples, '\n===')
17
18# X_1 X_0
19# 0 4.0 1.0
20# 1 0.0 0.0
21# 2 2.0 1.0
22# 3 1.0 0.0
23# ===
24# X_3 X_0 X_2 X_1
25# 0 0.0 0.0 0.0 1.0
26# 1 0.0 1.0 1.0 1.0
27# 2 0.0 0.0 0.0 1.0
28# 3 0.0 1.0 1.0 1.0
29# ===
30# X_1 X_3 X_4 X_0 X_2 X_5
31# 0 0.0 1.0 1.0 1.0 4.0 1.0
32# 1 0.0 0.0 1.0 1.0 0.0 1.0
33# 2 0.0 0.0 1.0 1.0 0.0 1.0
34# 3 1.0 0.0 1.0 1.0 0.0 2.0
35# ===
By passing the outline to the GuidelineIterator
rather than the Guideline
class, and calling the get_guidelines
, you get a generator that yields a guideline according to the outline, with only the exception of the directive which is specified by the route
argument: That argument is fixed (based on the step
argument for each iteration.
Note
To iterate over the distribution parameters coefficients, you can use the keywords bias, linear, interaction
. Example: route = 'nodes.normal.mu_.linear'