Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

np.random.permutation with seed?

Tags:

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 else? Thanks.

like image 516
Rockbar Avatar asked Dec 10 '17 19:12

Rockbar


People also ask

What NP random seed () does?

The numpy random seed is a numerical value that generates a new set or repeats pseudo-random numbers. The value in the numpy random seed saves the state of randomness. If we call the seed function using value 1 multiple times, the computer displays the same random numbers.

What is NP random permutation?

Random Permutations of Elements A permutation refers to an arrangement of elements. e.g. [3, 2, 1] is a permutation of [1, 2, 3] and vice-versa. The NumPy Random module provides two methods for this: shuffle() and permutation() .

How do you generate random permutations in Python?

To generate random Permutation in Python, then you can use the np random permutation. If the provided parameter is a multi-dimensional array, it is only shuffled along with its first index. If the parameter is an integer, randomly permute np.

What is seed in random state?

seed is a method to fill random. RandomState container. from numpy docs: numpy.random.seed(seed=None) Seed the generator. This method is called when RandomState is initialized.


2 Answers

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.

like image 89
Sebastian Mendez Avatar answered Sep 20 '22 14:09

Sebastian Mendez


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] 
like image 26
Giorgos Myrianthous Avatar answered Sep 19 '22 14:09

Giorgos Myrianthous