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

Viewed 32

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).

1 Answers

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.

Related