NumPy: Data Generation
✕1. Generating Different Matrix Types
- In Data Science, we often need to create arrays or matrices quickly. For example, we may need: - A matrix filled with zeros - A matrix filled with ones - A matrix filled with the same value - An identity matrix for mathematical operations NumPy provides ready-made functions for this. ⚡
1.1 🧱 Why Generate Matrices?
1.2 Matrix Generation Functions
| Function | Meaning | Example |
|---|---|---|
np.zeros() | Creates an array filled with zeros | np.zeros((2, 3)) => [[0., 0., 0.], [0., 0., 0.]] |
np.ones() | Creates an array filled with ones | np.ones((3, 2), dtype=int) => [[1, 1], [1, 1], [1, 1]] |
np.full() | Creates an array filled with a given value | np.full((2, 2), 7) => [[7, 7], [7, 7]] |
np.identity() | Creates an identity matrix | np.identity(2) => [[1., 0.], [0., 1.]] |
Examples for generating different types of matrices in NumPy

import numpy as npzeros_matrix = np.zeros((2, 3))ones_matrix = np.ones((3, 2), dtype=int)full_matrix = np.full((2, 2), 7)identity_matrix = np.identity(2)print("Zeros Matrix:\n", zeros_matrix)print("Ones Matrix:\n", ones_matrix)print("Full Matrix:\n", full_matrix)print("Identity Matrix:\n", identity_matrix)
1.3 Example
2. Generating Random Data
- Random data is useful when we want to: - Create sample data for practice - Test our code - Simulate real-world situations - Build examples for Machine Learning NumPy has many functions to generate random numbers.
2.1 🎲 Why Generate Random Data?
2.2 Random Data Generation Functions
| Function | Usage | Example |
|---|---|---|
np.random.rand() | Generate random decimal values from 0 up to 1 | np.random.rand(2, 3) |
np.random.seed() | Generate the same random data again | np.random.seed(42) |
np.random.randint() | Generate random whole numbers in given range | np.random.randint(1, 10, (2, 2)) |
np.random.normal() | Generate real-world-like data such as height, marks, or measurement errors | np.random.normal(20, 5, (2, 3)) |
np.random.choice() | Randomly picks from fixed options | np.random.choice([10, 20, 30], (2, 3)) |
Examples for generating random data in NumPy
import numpy as npnp.random.seed(42)# Makes the random output repeatablerandom_decimals = np.random.rand(2, 3)# 2 rows, 3 columnsrandom_integers = np.random.randint(1, 10, (2, 2))random_normal = np.random.normal(20, 5, (2, 3))random_choice = np.random.choice([10, 20, 30], (2, 3))print("Random Decimals:\n", random_decimals)print("Random Integers:\n", random_integers)print("Random Normal:\n", random_normal)print("Random Choice:\n", random_choice)
2.3 Example
3. Generating Sequences
- Sometimes we need numbers in a proper order or pattern. For example: - 0, 2, 4, 6, 8 - 0, 0.25, 0.5, 0.75, 1 - 2, 4, 8, 16, 32 NumPy provides functions to generate such sequences easily.
3.1 🔢 Why Generate Sequences?
3.2 Sequence Generation Functions
| Function | Usage | Example |
|---|---|---|
np.arange() | Creates values with a fixed step (like range) | np.arange(0, 10, 2) => [0 2 4 6 8] |
np.linspace() | Creates a fixed number of evenly spaced values | np.linspace(0, 1, 5) => [0. 0.25 0.5 0.75 1. ] |
np.geomspace() | Creates values using multiplicative spacing | np.geomspace(2, 32, 5) => [2. 4. 8. 16. 32.] |
Examples for generating sequences in NumPy

import numpy as nparange_seq = np.arange(0, 10, 2)linspace_seq = np.linspace(0, 1, 5)geomspace_seq = np.geomspace(2, 32, 5)print("Arange Sequence:\n", arange_seq)print("Linspace Sequence:\n", linspace_seq)print("Geomspace Sequence:\n", geomspace_seq)
3.3 Example
