2

This is my code:

random_idx = np.random.permutation(len(cIds))
train_Ids = cIds[random_idx[:train_size]]

Now, I want the list to be randomized in the same order every time I run this line of code.

Note: I do not want to save random_idx variable in a text file, and fetch the same list.

Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
John Doe
  • 437
  • 1
  • 5
  • 14
  • 2
    Possible duplicate of [random.seed(): What does it do?](https://stackoverflow.com/questions/22639587/random-seed-what-does-it-do) and [this comment](https://stackoverflow.com/questions/22639587/random-seed-what-does-it-do#comment74255398_22639752) is particularly useful. – pault Dec 22 '18 at 04:42
  • You need to use a `seed` as is demonstrated in [this answer](https://stackoverflow.com/questions/47742622/np-random-permutation-with-seed). – jason m Dec 22 '18 at 04:44
  • What is the range of seed value? like 0-100? -100 to 100? infinite ot infinite? Can it be negative? Fraction? – John Doe Dec 22 '18 at 04:46

2 Answers2

6

You can use seed in order to tell numpy to generate the same random numbers:

np.random.seed(seed=1234)
random_idx = np.random.permutation(len(cIds))

same as:

np.random.seed(1234)
random_idx = np.random.permutation(len(cIds))

Or

random_idx = np.random.RandomState(seed=1234).permutation(len(cIds)

seed: Must be convertible to 32 bit unsigned integers`

Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
1

Or you can do in in pandas style:

cIds.sample(n=train_size, 
            replace=False, 
            random_state=your_favorite_number_here)
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74