w3resource

NumPy: Uniform, non-uniform random sample from a given 1-D array with and without replacement


Random Sampling with/without Replacement

Write a NumPy program to generate a uniform, non-uniform random sample from a given 1-D array with and without replacement.

This problem involves writing a NumPy program to generate uniform and non-uniform random samples from a given one-dimensional array, both with and without replacement. The task requires utilizing NumPy's random sampling functions, such as "numpy.random.choice()", to efficiently generate samples according to specified parameters like replacement and probability weights. By providing options for sampling with or without replacement, the program facilitates diverse sampling techniques for statistical analysis and modeling tasks.

Sample Solution :

Python Code :

# Importing the NumPy library with an alias 'np'
import numpy as np 

# Printing a message indicating the generation of a uniform random sample with replacement
print("Generate a uniform random sample with replacement:") 

# Generating a random sample of 5 elements chosen from integers 0 to 6 (7 is exclusive) with replacement
print(np.random.choice(7, 5))

# Printing a message indicating the generation of a uniform random sample without replacement
print("\nGenerate a uniform random sample without replacement:") 

# Generating a random sample of 5 elements chosen from integers 0 to 6 without replacement
print(np.random.choice(7, 5, replace=False))

# Printing a message indicating the generation of a non-uniform random sample with replacement
print("\nGenerate a non-uniform random sample with replacement:") 

# Generating a random sample of 5 elements chosen from integers 0 to 6 with replacement, with custom probabilities
print(np.random.choice(7, 5, p=[0.1, 0.2, 0, 0.2, 0.4, 0, 0.1]))

# Printing a message indicating the generation of a non-uniform random sample without replacement
print("\nGenerate a uniform random sample without replacement:") 

# Generating a random sample of 5 elements chosen from integers 0 to 6 without replacement,
# with custom probabilities
print(np.random.choice(7, 5, replace=False, p=[0.1, 0.2, 0, 0.2, 0.4, 0, 0.1]))   

Output:

Generate a uniform random sample with replacement:
[5 4 4 1 5]

Generate a uniform random sample without replacement:
[1 4 0 3 2]

Generate a non-uniform random sample with replacement:
[4 4 3 0 6]

Generate a uniform random sample without replacement:
[1 4 6 0 3]

Explanation:

print(np.random.choice(7, 5)): This statement generates an array of 5 random integers from the range [0, 7) (7 is not included). The random integers are chosen with replacement, which means the same integer can be chosen more than once. The results are printed.

print(np.random.choice(7, 5, replace=False)): This line generates an array of 5 random integers from the range [0, 7) without replacement. This means that each integer in the range can only be chosen once. The results are printed.

print(np.random.choice(7, 5, p=[0.1, 0.2, 0, 0.2, 0.4, 0, 0.1])): This line generates an array of 5 random integers from the range [0, 7) with replacement and with custom probabilities for each integer. The probabilities are provided in the p parameter as a list [0.1, 0.2, 0, 0.2, 0.4, 0, 0.1]. The sum of all probabilities in the list should be equal to 1. The results are printed.

print(np.random.choice(7, 5, replace=False, p=[0.1, 0.2, 0, 0.2, 0.4, 0, 0.1])): This line generates an array of 5 random integers from the range [0, 7) without replacement and with custom probabilities for each integer, as specified in the p parameter. The results are printed.

Python-Numpy Code Editor: