[SOLVED] How to use random_state=np.random.RandomState(0)

Issue

This Content is from Stack Overflow. Question asked by Airwrecka

I’m writing a function like this
def split_into_train_and_test(x_all_LF, frac_test=0.5, random_state=None):

The function is passed in like this
split_into_train_and_test(x_LF, frac_test=0.3, random_state=np.random.RandomState(0))

Currently I am using this to get the random seed, but it’s not working.

if random_state is None:
       random_state = np.random
   else:
       np.random.seed(random_state)
       np.shuffle(x_all_LF)

How do I extract the random seed with np.random.RandomState(0).



Solution

Just use random_state instead of np.random and the functions in the np namespace:

if random_state is None:
   random_state = np.random
else:
   random_state.shuffle(x_all_LF)

The only methods that RandomState has that aren’t available in np.random are dunder methods and tomaxint.


This Question was asked in StackOverflow by Airwrecka and Answered by pigrammer It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?