简体   繁体   中英

How is numpy.choice different from numpy.randint?

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

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".

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM