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

Viewed 11115

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]
2 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

Documentation numpy.random.seed

np.random.seed(this_is_my_seed)

That could be an integer or a list of integers

np.random.seed(300)

Or

np.random.seed([3, 1415])

Example

np.random.seed([3, 1415])

service_code_options = ['899.59O', '12.42R', '13.59P', '204.68L']
np.random.choice(service_code_options, 3)

array(['899.59O', '204.68L', '13.59P'], dtype='<U7')

Notice that I passed a 3 to the choice function to specify the size of the array.

numpy.random.choice

Related