Sample With Replacement in Python

Use the
random.choices()
Function to Sample With Replacement in Python 
Use the
random.choice()
Function to Sample With Replacement in Python 
Use the
numpy.random.choice()
Function to Sample With Replacement in Python
Sampling refers to the process of selecting samples of data out of a given sequence. Several functions are available in the random
module to select a sample from a given sequence.
There is also a random
submodule within the numpy
package to work with random numbers in an array.
We can use the random.choice()
function to select a single random element. The random.sample()
function can sample without replacement.
The random.choices()
function is used for sampling with replacement in Python.
This tutorial demonstrates how to get a sample with replacement in Python. We will select the sample from a list of integers.
Use the random.choices()
Function to Sample With Replacement in Python
Python 3.6 introduced the random.choices()
function. This function is used to generate a sample with replacement in Python.
We can pass the list and the total number of elements required to get the final sample. The result is returned in a list.
For example:
import random
lst = [5,8,9,6,2,3,1,0,11,12,10]
print(random.choices(lst, k = 5))
Output:
[1, 11, 10, 5, 10]
In the above example, we create a sample with replacement in Python of length 5 from a list in Python.
We can also specify some weights using the weights
parameter to make the selections. The cum_weights
can also make selections based on the cumulative weights.
The weights get converted to cumulative weights internally.
Use the random.choice()
Function to Sample With Replacement in Python
As discussed in previous sections, the random.choice()
selects a random element from a provided sequence.
We can run the for
loop to generate a list with randomly selected elements. Since the function will run in every loop, elements will get selected without knowing the previously selected element.
For example:
import random
lst = [5,8,9,6,2,3,1,0,11,12,10]
result = [random.choice(lst) for _ in range(5)]
print(result)
Output:
[2, 0, 0, 12, 6]
We use list comprehension to create a list and store randomly selected elements (generated by the random.choice()
function) in this list.
Use the numpy.random.choice()
Function to Sample With Replacement in Python
There is a random
submodule in the numpy
package. We can use the numpy.random.choice()
function to sample with replacement in Python.
The numpy.random.choice()
function selects a given number of elements from a onedimensional numpy
array. The final result is returned in a numpy
array.
This function accepts a parameter called replace
(True
by default). If this parameter is changed to False
, the sample is returned without replacement.
We will generate a sample with replacement using this function in the example below.
import numpy
lst = [5,8,9,6,2,3,1,0,11,12,10]
arr = numpy.array(lst)
print(numpy.random.choice(arr, 5))
Output:
[11 10 6 9 3]
To wrap up, we discussed several methods to generate a sample with replacement in Python. The random.choices()
function is the most straightforward option, but it works only with Python 3.6 and above.
For previous versions, we can either use the random.choice()
or the numpy.random.choice()
function.
Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.
LinkedIn