Getting Started with numpy.random
Before diving into specific functions, let’s import the necessary module:
import numpy as np
The numpy.random
module (now a submodule of numpy.random
) offers a wide array of functions for generating various types of random numbers. Note that numpy.random
was redesigned in version 1.17. Older code using np.random
directly might need adjustment.
Generating Random Numbers from Different Distributions
NumPy provides functions to sample from many common probability distributions. Here are a few examples:
Uniform Distribution
The rand()
function generates random numbers from a uniform distribution over the interval [0, 1):
= np.random.rand(5)
random_numbers print(random_numbers)
= np.random.rand(3, 3)
random_array print(random_array)
To generate random numbers from a uniform distribution within a specific range (a, b), use uniform()
:
= np.random.uniform(2, 10, 10)
random_numbers_range print(random_numbers_range)
Normal (Gaussian) Distribution
The randn()
function generates random numbers from a standard normal distribution (mean=0, standard deviation=1):
= np.random.randn(5)
standard_normal print(standard_normal)
#Generate a 2x4 array of numbers from a normal distribution with mean 5 and standard deviation 2
= np.random.normal(loc=5, scale=2, size=(2,4))
normal_array print(normal_array)
Other Distributions
NumPy offers functions for numerous other distributions, including:
randint()
: Generates random integers from a specified range.poisson()
: Generates random numbers from a Poisson distribution.binomial()
: Generates random numbers from a binomial distribution.exponential()
: Generates random numbers from an exponential distribution. And many more! Consult the NumPy documentation for a complete list.
Seeding the Random Number Generator
Reproducibility is key in scientific computing. You can set a seed using seed()
to ensure that the sequence of random numbers generated is the same every time you run your code:
42) #Sets the seed to 42
np.random.seed(= np.random.rand(5)
random_numbers print(random_numbers)
42) #Same seed, same output
np.random.seed(= np.random.rand(5)
random_numbers_again print(random_numbers_again)
Changing the seed will produce a different sequence.
Generating Random Integers
The randint()
function is very useful for generating random integers:
#Generate 5 random integers between 1 and 10 (inclusive)
= np.random.randint(1, 11, 5)
random_integers print(random_integers)
Choosing Random Elements from Arrays
NumPy also provides functions for selecting random elements from existing arrays. choice()
is particularly useful:
= np.array([1, 2, 3, 4, 5])
my_array #Choose one random element from my_array
= np.random.choice(my_array)
random_element print(random_element)
#Choose 3 random elements with replacement
= np.random.choice(my_array, size=3, replace=True)
random_elements_replacement print(random_elements_replacement)
#Choose 3 random elements without replacement
= np.random.choice(my_array, size=3, replace=False)
random_elements_no_replacement print(random_elements_no_replacement)
Shuffling Arrays
The shuffle()
function randomly permutes the elements of an array in place:
= np.array([1, 2, 3, 4, 5])
my_array
np.random.shuffle(my_array)print(my_array) #The original array is modified
Remember that shuffle
modifies the array directly; it doesn’t return a new shuffled array. To obtain a shuffled copy without modifying the original, you can use permutation()
.
= np.array([1, 2, 3, 4, 5])
my_array = np.random.permutation(my_array)
shuffled_array print(shuffled_array)
print(my_array) #Original array is unchanged
These examples provide a solid foundation for working with NumPy’s random number generation capabilities. Experiment with these functions and explore the extensive documentation to unlock the full potential of NumPy’s random number tools for your data science projects.