Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a random seed while using Python's numpy random choice?

I have a list of four strings. Then in a Pandas dataframe I want to create a variable randomly selecting a value from this list and assign into each row. I am using numpy's random choice, but reading their documentation, there is no seed option. How can I specify the random seed to the random assignment so every time the random assignment will be the same?

service_code_options = ['899.59O', '12.42R', '13.59P', '204.68L']
df['SERVICE_CODE'] = [np.random.choice(service_code_options ) for i in df.index]
like image 690
KubiK888 Avatar asked Oct 25 '18 14:10

KubiK888


People also ask

How do you seed random choices in Python?

The random module uses the seed value as a base to generate a random number. Use a random. seed() function with other random module functions to reproduce their output again and again.

How do you define a seed in Python?

Definition and UsageThe seed() method is used to initialize the random number generator. The random number generator needs a number to start with (a seed value), to be able to generate a random number. By default the random number generator uses the current system time.

What does NP random seed () do?

NumPy random seed is simply a function that sets the random seed of the NumPy pseudo-random number generator. It provides an essential input that enables NumPy to generate pseudo-random numbers for random processes.


1 Answers

You need define it before by numpy.random.seed, also list comprehension is not necessary, because is possible use numpy.random.choice with parameter size:

np.random.seed(123)

df = pd.DataFrame({'a':range(10)})

service_code_options = ['899.59O', '12.42R', '13.59P', '204.68L']
df['SERVICE_CODE'] = np.random.choice(service_code_options, size=len(df))
print (df)
   a SERVICE_CODE
0  0       13.59P
1  1       12.42R
2  2       13.59P
3  3       13.59P
4  4      899.59O
5  5       13.59P
6  6       13.59P
7  7       12.42R
8  8      204.68L
9  9       13.59P
like image 50
jezrael Avatar answered Sep 22 '22 01:09

jezrael