Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a seed to pd.sample like np.random.seed?

Tags:

python

pandas

I have a panda dataframe and I want to randomly select several columns from it. And I want to select the same columns every time. I find there is a seed moduel for numpy.random but I do not know any similar application in pandas.

like image 789
Liming Zhu Avatar asked Apr 18 '19 14:04

Liming Zhu


People also ask

What is seed in NP random seed?

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 seed () in Python?

The 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 is seed in random state?

Seed is a global pseudo-random generator. However, randomstate is a pseudo-random generator isolated from others, which only impact specific variable.


1 Answers

You can use a parameter random_state. See example below taken from documentation: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sample.html

df['num_legs'].sample(n=3, random_state=1)

It will ensure that 3 random data will be used every time you run it. Then you can change the value random_state as you want

like image 141
jose_bacoy Avatar answered Oct 21 '22 13:10

jose_bacoy