Creating fixed-size arrays and generating random numbers are important operations for building test data and performing numerical simulations. These techniques help initialize datasets and introduce randomness for analysis, modelling and experimentation.
Creating Fixed-Size Arrays
NumPy provides built-in functions to create arrays of a fixed shape filled with predefined values such as zeros, ones or identity matrices. These functions are efficient and widely used for initializing arrays before computation.
1. Zeros: To create an array of a specified shape zeros() function is used, where all elements are initialised to zero based on the given shape. It is often used as a starting point for numerical calculations or matrix operations. The dtype parameter controls the data type of the elements.
import numpy as np
print(np.zeros(10))
print(np.zeros((2, 4)))
print(np.zeros((2, 3), dtype=int))
Output
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [[0. 0. 0. 0.] [0. 0. 0. 0.]] [[0 0 0] [0 0 0]]
2. Ones: ones() function works similarly to zeros(), but all elements are initialized to one. It is commonly used when default non-zero values are required.
import numpy as np
print(np.ones(5))
print(np.ones((2, 3), dtype=int))
Output
[1. 1. 1. 1. 1.] [[1 1 1] [1 1 1]]
3. Eye (Identity Matrix): eye() function generates an identity matrix, where diagonal elements are 1 and all other elements are 0. Identity matrices are frequently used in linear algebra.
import numpy as np
print(np.eye(3))
print(np.eye(3, 4, dtype=int))
Output
[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] [[1 0 0 0] [0 1 0 0] [0 0 1 0]]
Random Number Generation
NumPy includes a random module that can generate numbers from different probability distributions. These functions are useful for simulations, machine learning and testing algorithms.
1. Random Numbers (Uniform Distribution): rand() function generates random floating-point numbers uniformly distributed between 0 and 1.
- np.random.rand() returns a single random float between 0 and 1.
- np.random.rand(5) generates a 1-D array of 5 random values.
- np.random.rand(3, 3) creates a 2-D array (3 rows × 3 columns) filled with random values between 0 and 1.
import numpy as np
print(np.random.rand())
print(np.random.rand(5))
print(np.random.rand(3, 3))
Output
0.3128801293275312 [0.50825151 0.23863536 0.02606872 0.26528772 0.82513609] [[0.9284381 0.71266708 0.02207328] [0.78124974 0.54578777 0.31524056] [0.34894802 0.38937012 0.08736173]]
2. Random Integers: randint() function generates random integers within a specified range.
- np.random.randint(10) generates one random integer between 0 and 9.
- np.random.randint(10, size=5) produces five random integers between 0 and 9.
- np.random.randint(10, 20, size=(2, 3)) generates a 2 × 3 array of random integers between 10 (inclusive) and 20 (exclusive).
import numpy as np
print(np.random.randint(10))
print(np.random.randint(10, size=5))
print(np.random.randint(10, 20, size=(2, 3)))
Output
4 [4 9 2 7 5] [[11 14 15] [18 15 14]]
3. Random Numbers from Normal Distribution: randn() function generates numbers following a normal (Gaussian) distribution with mean 0 and variance 1.
- np.random.randn() returns a single normally distributed value.
- np.random.randn(2, 3) creates a 2 × 3 array of values centered around 0 with both positive and negative numbers.
import numpy as np
print(np.random.randn())
print(np.random.randn(2, 3))
Output
-1.0199881391778292 [[ 1.06210503 -0.78144036 -0.83011667] [-0.45468798 0.94735935 1.25590952]]
4. Random Numbers from Other Probability Distributions: NumPy also supports additional distributions such as uniform and binomial.
- np.random.uniform(size=(2, 2)) generates a 2 × 2 array of random floating-point numbers uniformly distributed between 0 and 1.
- np.random.binomial(10, 0.5, size=(2, 2)) simulates binomial experiments, where 10 is the number of trials, 0.5 is the probability of success and result shows how many successes occurred in each experiment.
import numpy as np
print(np.random.uniform(size=(2, 2)))
print(np.random.binomial(10, 0.5, size=(2, 2)))
Output
[[0.24817849 0.91612252] [0.91785589 0.08758948]] [[5 6] [4 4]]