Mastering np.random.uniform for Random Sampling
Comprehensive Guide to np.random.uniform in Python
np.random.uniform is a NumPy function that generates random numbers uniformly distributed within a specified range. This function is commonly used in simulations, random sampling, and initializing values in machine learning. The uniform distribution ensures that every number in the range has an equal probability of being generated.
Syntax:
np.random.uniform(low=0.0, high=1.0, size=None)
Parameters:
- low (float, optional): The lower bound of the range (inclusive). Default is 0.0.
- high (float, optional): The upper bound of the range (exclusive). Default is 1.0.
- size (int or tuple of ints, optional): Specifies the shape of the output. If None, a single float is returned. Otherwise, an array is returned with the given shape.
Returns:
- ndarray or float: Random numbers uniformly sampled from the interval [low, high).
Examples:
Example 1: Generate a Single Random Number
Code:
import numpy as np
# Generate a random number between 0 and 1
random_number = np.random.uniform()
# Print the generated number
print("Random number between 0 and 1:", random_number)
Output:
Random number between 0 and 1: 0.2576756947893093
Explanation:
This generates a single float between 0 (inclusive) and 1 (exclusive). No additional parameters are needed as these are the default bounds.
Example 2: Generate Multiple Random Numbers in a Custom Range
Code:
import numpy as np
# Generate 5 random numbers between 10 and 20
random_numbers = np.random.uniform(low=10, high=20, size=5)
# Print the generated numbers
print("Random numbers between 10 and 20:", random_numbers)
Output:
Random numbers between 10 and 20: [14.68053023 17.09450566 12.63608249 15.4366088 17.70240744]
Explanation:
The range is specified using the low and high parameters, and the size parameter defines the number of random numbers to generate
Example 3: Create a 2D Array of Random Numbers
Code:
import numpy as np
# Create a 3x3 array of random numbers between -5 and 5
random_array = np.random.uniform(low=-5, high=5, size=(3, 3))
# Print the generated array
print("3x3 array of random numbers between -5 and 5:\n", random_array)
Output:
3x3 array of random numbers between -5 and 5: [[-0.96240787 4.51614896 -0.59872793] [-0.65079602 1.67049096 -0.49317933] [-2.29151729 0.84020872 -4.18445526]]
Explanation:
A 3x3 array is generated, with each element uniformly sampled from the range [-5, 5).
Example 4: Set a Seed for Reproducibility
Code:
import numpy as np
# Set a random seed for reproducibility
np.random.seed(42)
# Generate a random number between 0 and 1
random_number = np.random.uniform()
# Print the generated number
print("Random number with seed 42:", random_number)
Output:
Random number with seed 42: 0.3745401188473625
Explanation:
Setting a seed ensures that the random numbers generated are consistent across multiple runs, which is useful for debugging and testing.
Example 5: Generate Random Numbers for Machine Learning Initialization
Code:
import numpy as np
# Simulate initializing weights for a neural network
weights = np.random.uniform(low=-1, high=1, size=(4, 3))
# Print the weight matrix
print("Randomly initialized weights for a 4x3 layer:\n", weights)
Output:
Randomly initialized weights for a 4x3 layer: [[ 0.90142861 0.46398788 0.19731697] [-0.68796272 -0.68801096 -0.88383278] [ 0.73235229 0.20223002 0.41614516] [-0.95883101 0.9398197 0.66488528]]
Explanation:
Randomly initializing weights within a small range (e.g., -1 to 1) is a common use case in training machine learning models.
Additional Notes:
1. Performance: np.random.uniform is optimized for generating random numbers in bulk, making it suitable for large datasets.
2. Applications: Used in Monte Carlo simulations, games, machine learning, and random sampling tasks.
3. Data Types: The output data type is float64 by default. Use astype to convert to other types if needed.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics