Skip to content Skip to sidebar Skip to footer

Np.random.permutation With Seed?

I want to use a seed with np.random.permutation, like np.random.permutation(10, seed=42) I get the following error: 'permutation() takes no keyword arguments' How can I do that e

Solution 1:

If you want it in one line, you can create a new RandomState, and call the permutation on that:

np.random.RandomState(seed=42).permutation(10)

This is better than just setting the seed of np.random, as it will have only a localized effect.


Solution 2:

np.random.seed(42)
np.random.permutation(10)

If you want to call np.random.permutation(10) multiple times and get identical results, you also need to call np.random.seed(42) every time you call permutation().


For instance,

np.random.seed(42)
print(np.random.permutation(10))
print(np.random.permutation(10))

will produce different results:

[8 1 5 0 7 2 9 4 3 6]
[0 1 8 5 3 4 7 9 6 2]

while

np.random.seed(42)
print(np.random.permutation(10))
np.random.seed(42)
print(np.random.permutation(10))

will give the same output:

[8 1 5 0 7 2 9 4 3 6]
[8 1 5 0 7 2 9 4 3 6]

Solution 3:

Set the seed in the previous line

np.random.seed(42)
np.random.permutation(10)

Solution 4:

You can break it down into:

import numpy as np
np.random.seed(10)
np.random.permutation(10)

By initializing the random seed first, this will guarantee that you get the same permutation.


Post a Comment for "Np.random.permutation With Seed?"