NumPy: Data Generation

Generating Different Matrix types

Matrix Generation Functions in NumPy:
FunctionDescriptionExample
np.zerosGenerates an array filled with zeros.np.zeros((2, 3)) => [[0., 0., 0.], [0., 0., 0.]]
np.onesGenerates an array filled with ones.np.ones((3, 2), dtype=int) => [[1, 1], [1, 1], [1, 1]]
np.fullGenerates an array filled with given value.np.full((2, 2), 7) => [[7, 7], [7, 7]]
np.identityGenerates an identity matrix.np.identity(3) => [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]
Examples for generating different types of matrices in NumPy

Generating Random Data

Random Data Generation Functions in NumPy:
FunctionDescriptionExample
np.random.randGenerates an array of given shape with random values between 0 and 1.np.random.rand(2, 3) => [[0.5488135, 0.71518937, 0.60276338], [0.54488318, 0.4236548, 0.64589411]]
np.random.seedSets the seed for random number generation to ensure reproducibility.np.random.seed(42)
np.random.randintGenerates an array of given shape with random integers from a specified range.np.random.randint(1, 10, size=(2, 2)) => [[5, 8], [9, 5]]
np.random.normalGenerates an array of given shape with random samples from a normal distribution.np.random.normal(20, 5, size=(2, 3)) => [[18.23, 22.45, 19.87], [21.34, 17.56, 24.78]]
np.random.choiceGenerates an array of given shape by randomly sampling from a given 1-D array.np.random.choice([10, 20, 30], size=(2, 3)) => [[20, 10, 30], [10, 20, 20]]
Examples for generating random data in NumPy

Generating Sequences

Sequence Generation Functions in NumPy:
FunctionDescriptionExample
np.arangeEvenly spaced values within a given interval. (like range)np.arange(0, 10, 2) => [0, 2, 4, 6, 8]
np.linspaceEvenly spaced values between a specified start and end.np.linspace(0, 1, 5) => [0., 0.25, 0.5, 0.75, 1.]
np.geomspaceNumbers spaced evenly on a geometric progression.np.geomspace(2, 32, 5) => [2., 4., 8., 16., 32.]
Examples for generating sequences and grids in NumPy

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.