Is there a Python equivalent to R's sample() function?

Bilal picture Bilal · Dec 3, 2015 · Viewed 8.4k times · Source

I want to know if Python has an equivalent to the sample() function in R.

The sample() function takes a sample of the specified size from the elements of x using either with or without replacement.

The syntax is:

sample(x, size, replace = FALSE, prob = NULL)

(More information here)

Answer

Julian Wittische picture Julian Wittische · Dec 3, 2015

I think numpy.random.choice(a, size=None, replace=True, p=None) may well be what you are looking for.

The p argument corresponds to the prob argument in the sample()function.