0

I need to sample with replacement from an array A of length n. I want to know how the below two commands are different. If they both give the same result which one is better (in terms of performance etc...)

A[np.random.randint(0, n, n)]

A[np.random.choice(n, n)]
3
  • Have you tried timing them? Commented Apr 20, 2019 at 10:55
  • Usually Choice seems faster for n = 1 million, but in some of the trials, choice took slightly longer Commented Apr 20, 2019 at 11:10
  • In my quick time tests, they were about the same for large n, while choice took longer for small n. They probably both use the same random number generator, but choice has a longer setup time - after all it accepts more parameters. But the code for both is compiled, which is hard to study. Commented Apr 20, 2019 at 16:22

3 Answers 3

1

The purpose of choice is to sample an array, giving it an integer is a shortcut to giving it a range of that integer's length. So randint is likely going to be more efficient if you're misusing choice the way you are.

However the correct way to do it is np.random.choice(A, size=n). That's exactly how you spell "sample with replacement".

Sign up to request clarification or add additional context in comments.

Comments

1

randint returns a random integer in the provided range. choice returns a random element from a provided array, or if you provide an int (like u did) it functions like np.random.randint(0, n, n). So, in this example there's no difference however I imagine randint would be slightly faster.

Comments

1

Both numpy.randon.randint and numpy.random.choice gives you an option to select random numbers either from a range(in case of randint) or from an array(in case of choice). When your array has items in a range, then The main difference of using numpy.random.choice is to:

  • Specify if you want the outcomes with or without replacement from the array.
  • Specify the probabilities associated with each entry in the array.

outcome of one coin flip

  • np.random.randint(2)

outcomes of ten thousand coin flips

  • np.random.randint(2, size=10000)

outcome of one coin flip

  • np.random.choice([0, 1])

outcome of ten thousand coin flips

  • np.random.choice([0, 1], size=10000)

outcomes of ten thousand biased coin flips

  • np.random.choice([0, 1], size=10000, p=[0.8, 0.2])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.