Amn Kh - 1 year ago 95

Python Question

I am using DEAP toolbox in Python for Genetic Algorithm.

`toolbox.register("attr_bool", random.randint, 0, 1)`

I think

`srng.binomial(X.shape, p=retain_prob)`

`random.randint`

Answer Source

The arguments to `toolbox.register`

must be a function and the arguments that you want to pass to that function when you run it

Since `0 if random.randint(0, 4) == 0 else 1`

is not a function (its a random number) you got an error. The fix is to package this expression inside a function that you can pass to `toolbox.register`

:

```
# returns 1 with probability p and 0 with probability 1-p
def bernoulli(p):
if random.random() < p:
return 1
else:
return 0
toolbox.register("attr_bool", bernoulli, 0.8)
```