Ukiyo
Ukiyo

Reputation: 21

Get a non-uniform sample with numpy

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.

Upvotes: 2

Views: 2514

Answers (2)

stevemo
stevemo

Reputation: 1097

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

Upvotes: 0

Mykola Semenov
Mykola Semenov

Reputation: 802

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)

Upvotes: 1

Related Questions