Get a non-uniform sample with numpy

Viewed 901

I would like to get a non-uniform sample (500 int) with numpy. I tried numpy.random.randint, but I get a uniform sample. Any simple solutions ? Thanks for yours answers.

2 Answers

You can find a lot of non-uniform distributions in scipy.stats, and use them in such way:

from scipy.stats import <distribution_you_want>

sample = <distribution_you_want>.rvs(size=500)

If you just want to generate samples from a distribution, staying in NumPy is probably easier. (Scipy provides methods for the PDFs themselves, which can become more complicated.) There are dozens of non-uniform distributions to choose from in the numpy.random module. For example, if you're after discrete, integer, nonnegative samples:

sample = np.random.poisson(5, size=1000)
plt.hist(sample)

enter image description here

Related