NumPy Random Module
NumPy's random module is a powerful tool for generating random numbers, permutations, and various probability distributions. It's widely used in simulations, statistics, and machine learning to introduce randomness into computations.
Random Number Generation:
np.random.rand()
This function generates random numbers from a uniform distribution in the interval 0 to 1. This means that all values in the output array are equally likely, and there is no clustering around any particular value. The generated numbers are between 0 (inclusive) and 1 (exclusive). It's useful for generating random data that doesn't necessarily follow a specific distribution.
import numpy as np
# Generate random numbers from a uniform distribution [0, 1)
random_numbers = np.random.rand(3, 2)
print(random_numbers)
np.random.randint()
This function generates random integers within a specified range. You can specify the lower bound (inclusive) and upper bound (exclusive) of the range. The size
parameter determines the shape of the output array.
# Generate random integers in the range [1, 10) with size 5
random_integers = np.random.randint(1, 10, size=5)
print(random_integers) # [2 3 4 8 6]
You can also generate 2D array
# 2D array of shape (2, 3) with random integers between 0 and 10
random_integers_2d = np.random.randint(low=0, high=10, size=(2, 3))
print(random_integers_2d)
np.random.randn()
This function generates random numbers from a standard normal distribution with mean 0 and standard deviation 1. The generated values are more likely to be around 0, with tails tapering off as they move away from 0.
# Generate random numbers from standard normal distribution (mean=0, std=1)
random_normal = np.random.randn(3, 2)
print(random_normal)
np.random.random()
This function is similar to np.random.rand()
. It generates random numbers from a uniform distribution over the interval 0 to 1
# Generate a single random numbers from a uniform distribution [0, 1)
random_number = np.random.random()
print(random_numbers) # 0.9679494167233668
Permutations
np.random.shuffle()
The np.random.shuffle
function is used to shuffle the elements of the array randomly. The shuffle
function modifies the array in-place, rearranging its elements in a random order.
array = np.array([1, 2, 3, 4, 5])
np.random.shuffle(array)
print(array) # [3 2 4 1 5]
np.random.permutation()
This function returns a shuffled copy of the input array. It doesn't modify the original array as np.random.shuffle()
does
array = np.array([1, 2, 3, 4, 5])
shuffled_array = np.random.permutation(array)
print(shuffled_array) # [5 3 1 2 4]
print(array) # [1 2 3 4 5]
Distributions
np.random.uniform()
This function generates random numbers from a uniform distribution over a specified interval (low, high).
# Generating a uniform distribution
# Parameters: low = 0, high = 1
# Generating 5 random samples from the uniform distribution
uniform_distribution = np.random.uniform(0, 1, size=5)
# Printing the generated uniform distribution
print(uniform_distribution)
np.random.normal()
This function generates random numbers from a normal distribution with specified mean and standard deviation.
# Generating a normal distribution
# Parameters: mean = 10, standard deviation = 2
# Generating 5 random samples from the normal distribution
normal_distribution = np.random.normal(10, 2, size=5)
# Printing the generated normal distribution
print(normal_distribution)
np.random.binomial()
This function generates random numbers from a binomial distribution. It simulates the number of successes in a fixed number of independent Bernoulli trials.
# Generating a binomial distribution
# Parameters: n = 10 (number of trials), p = 0.5 (probability of success)
# Generating 5 random samples from the binomial distribution
binomial_distribution = np.random.binomial(10, 0.5, size=5)
# Printing the generated binomial distribution
print(binomial_distribution)
Summary of Distributions
Here is a table summarizing the main features of the uniform, normal, and binomial distributions and their corresponding functions in NumPy:
Distribution | Main Features | NumPy Function | Syntax |
---|---|---|---|
Uniform | All outcomes are equally likely within a given interval. | np.random.uniform() | np.random.uniform(low, high, size) |
Normal (Gaussian) | Data is symmetrically distributed around the mean, forming a bell-shaped curve. | np.random.normal() | np.random.normal(mean, std_dev, size) |
Binomial | Number of successes in a sequence of n independent experiments. | np.random.binomial() | np.random.binomial(n, p, size) |
In the syntax:
low
andhigh
specify the range of the uniform distribution.mean
andstd_dev
specify the mean and standard deviation of the normal distribution.n
andp
specify the number of trials and the probability of success for the binomial distribution.size
specifies the output shape for all three functions.