NumPy
stands as a cornerstone library. One of the useful functions provided by NumPy is numpy.uniform
, which allows users to generate random numbers from a uniform distribution. A uniform distribution is a probability distribution where every value within a specified range has an equal likelihood of being selected. This blog post will explore the fundamental concepts of numpy.uniform
, its usage methods, common practices, and best practices.numpy.uniform
numpy.uniform
The numpy.uniform
function is part of the numpy.random
module. It draws samples from a uniform distribution. The probability density function (PDF) of a continuous uniform distribution on the interval [a, b]
is given by:
[f(x)=\frac{1}{b - a}] for (a\leq x\leq b) and (0) otherwise.
In the context of numpy.uniform
, we can specify the lower (low
) and upper (high
) bounds of the interval from which the random numbers will be drawn. The general syntax of the function is:
numpy.random.uniform(low=0.0, high=1.0, size=None)
low
: The lower boundary of the output interval. All values generated will be greater than or equal to low
. The default value is 0.0
.high
: The upper boundary of the output interval. All values generated will be less than high
. The default value is 1.0
.size
: The shape of the output array. If None
, a single value is returned.import numpy as np
# Generate a single random number between 0 and 1
single_random_num = np.random.uniform()
print("Single random number:", single_random_num)
# Generate a single random number between 5 and 10
single_random_num_5_10 = np.random.uniform(low=5, high=10)
print("Single random number between 5 and 10:", single_random_num_5_10)
import numpy as np
# Generate an array of 5 random numbers between 0 and 1
random_array = np.random.uniform(size=5)
print("Array of 5 random numbers between 0 and 1:", random_array)
# Generate a 2D array (3x3) of random numbers between -2 and 2
random_2d_array = np.random.uniform(low=-2, high=2, size=(3, 3))
print("2D array (3x3) of random numbers between -2 and 2:\n", random_2d_array)
numpy.uniform
can be used to simulate various real - world experiments. For example, let’s simulate the rolling of a fair six - sided die. We can generate random numbers between 1 and 7 and then take the floor value.
import numpy as np
# Simulate rolling a die 10 times
die_rolls = np.floor(np.random.uniform(low=1, high=7, size=10)).astype(int)
print("Results of 10 die rolls:", die_rolls)
In neural networks, initializing weights randomly is a common practice. We can use numpy.uniform
to initialize the weights of a neural network layer within a certain range.
import numpy as np
# Number of input neurons
input_neurons = 5
# Number of output neurons
output_neurons = 3
# Initialize weights randomly between -0.1 and 0.1
weights = np.random.uniform(low=-0.1, high=0.1, size=(input_neurons, output_neurons))
print("Initialized weights:\n", weights)
When working on experiments or code that requires reproducibility, it is important to set a seed for the random number generator. This ensures that the same sequence of random numbers is generated every time the code is run.
import numpy as np
# Set the seed
np.random.seed(42)
random_numbers = np.random.uniform(size=3)
print("Random numbers with seed 42:", random_numbers)
# Set the seed again to reproduce the same results
np.random.seed(42)
random_numbers_again = np.random.uniform(size=3)
print("Random numbers with seed 42 (again):", random_numbers_again)
When using numpy.uniform
, make sure to choose appropriate lower and upper bounds based on the problem you are trying to solve. For example, if you are simulating a physical quantity that cannot be negative, set the lower bound to 0.
numpy.uniform
is a powerful and versatile function in the NumPy library for generating random numbers from a uniform distribution. It has a wide range of applications, from simulating experiments to initializing neural network weights. By understanding its fundamental concepts, usage methods, common practices, and best practices, you can effectively utilize this function in your scientific computing and data analysis projects.