NumPy: Data Generation

1. Generating Different Matrix Types

    1.1 🧱 Why Generate Matrices?
    1. 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.2 Matrix Generation Functions
FunctionMeaningExample
np.zeros()Creates an array filled with zerosnp.zeros((2, 3)) => [[0., 0., 0.], [0., 0., 0.]]
np.ones()Creates an array filled with onesnp.ones((3, 2), dtype=int) => [[1, 1], [1, 1], [1, 1]]
np.full()Creates an array filled with a given valuenp.full((2, 2), 7) => [[7, 7], [7, 7]]
np.identity()Creates an identity matrixnp.identity(2) => [[1., 0.], [0., 1.]]
Examples for generating different types of matrices in NumPy
NumPy Matrix Generation
NumPy Matrix Generation
    1.3 Example
    1. 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)

2. Generating Random Data

    2.1 🎲 Why Generate Random Data?
    1. 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.2 Random Data Generation Functions
FunctionUsageExample
np.random.rand()Generate random decimal values from 0 up to 1np.random.rand(2, 3)
np.random.seed()Generate the same random data againnp.random.seed(42)
np.random.randint()Generate random whole numbers in given rangenp.random.randint(1, 10, (2, 2))
np.random.normal()Generate real-world-like data such as height, marks, or measurement errorsnp.random.normal(20, 5, (2, 3))
np.random.choice()Randomly picks from fixed optionsnp.random.choice([10, 20, 30], (2, 3))
Examples for generating random data in NumPy
    2.3 Example
    1. import numpy as npnp.random.seed(42) # Makes the random output repeatable random_decimals = np.random.rand(2, 3) # 2 rows, 3 columns random_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)

3. Generating Sequences

    3.1 🔢 Why Generate Sequences?
    1. 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.2 Sequence Generation Functions
FunctionUsageExample
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 valuesnp.linspace(0, 1, 5) => [0. 0.25 0.5 0.75 1. ]
np.geomspace()Creates values using multiplicative spacingnp.geomspace(2, 32, 5) => [2. 4. 8. 16. 32.]
Examples for generating sequences in NumPy
NumPy Sequence Generation
NumPy Sequence Generation
    3.3 Example
    1. 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)
Advertisement
Ad PlaceholderSlot: 7421026683

Practice QuestionsNot started

  1. Random Data Generation

    Question 1 of 2

    • Set the random seed to 42 for reproducibility.
    • Generate a 3x3 array of random integers between 1 and 100.
    • Create a 4x4 array of random samples from a standard normal distribution.
    • Generate a 5x5 identity matrix.
    • Create 5x4 array with values from normal distribution of mean 50, std 10.
    • Create a sequence of 10 evenly spaced values between 0 and 1.
    • Generate a 2x3 array of random choices from the list [10, 20, 30].
  2. Mock Data Generation

    Question 2 of 2

    • Generate csv for student data with fields: RollNo, FirstName, LastName, Age, Height. - Use seed 101 for reproducibility. - Generate data for 100 students (using variable data_size = 100). - RollNo: Unique integers sequentially from 1 to data_size. - FirstName, LastName: Randomly choose value from pre-defined lists. - Age: Random integers between 18 and 25. - Height: From Normal distribution with mean 160.0, std 20.0 cm. - Rating: Random floats between 1.0 and 5.0. Hint: Generate each column separately using appropriate NumPy functions. Use zip to combine them. Use csv.writer to save combined data into csv.
Advertisement
Ad PlaceholderSlot: 5413242224