Skip to content

Latest commit

 

History

History
94 lines (73 loc) · 4.08 KB

File metadata and controls

94 lines (73 loc) · 4.08 KB

Numpy Question

Basic Array Creation

  1. Create an array with 10 equally spaced numbers between 0 and 1.
  2. Create a 1D array of 20 random integers between 1 and 100.
  3. Create a 2D array with the shape (4, 4) filled with the value 7.
  4. Create a 1D array of 10 elements with the same value, 3.14.
  5. Create a 2D array with the shape (3, 3) with values ranging from 1 to 9.

Array Operations

  1. Create two arrays: a = [1, 3, 5, 7, 9] and b = [2, 4, 6, 8, 10]. Perform element-wise subtraction.
  2. Multiply array a by 3: a = [2, 4, 6, 8].
  3. Divide array a by 2: a = [10, 20, 30, 40].
  4. Find the element-wise modulus of two arrays: a = [5, 10, 15], b = [2, 3, 4].
  5. Create two arrays: a = [1, 2, 3], b = [4, 5, 6]. Compute the dot product.

Matrix Multiplication

  1. Create two matrices: a = [[2, 3], [4, 5]] and b = [[1, 0], [0, 1]]. Perform matrix multiplication.
  2. Multiply a 3x3 matrix with a 3x1 vector.
  3. Create a 4x4 identity matrix and multiply it by a 4x4 matrix filled with random integers.
  4. Perform matrix multiplication on two 3x3 matrices: a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] and b = [[9, 8, 7], [6, 5, 4], [3, 2, 1]].
  5. Create a 2x3 matrix and a 3x2 matrix. Perform matrix multiplication.

Broadcasting

  1. Create a 1D array: array = [10, 20, 30]. Subtract a scalar value, 5, using broadcasting.
  2. Create a 3x3 array and a 3x1 array. Add them using broadcasting.
  3. Divide a 4x4 array by a scalar value, 2, using broadcasting.
  4. Create a 3x3 array and add 3 to each element using broadcasting.
  5. Multiply a 1D array of shape (5,) by a 2D array of shape (5, 5) using broadcasting.

Statistical Operations

  1. Calculate the median of the array: data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].
  2. Calculate the mode of the array: data = [1, 2, 2, 3, 4, 4, 4, 5, 6].
  3. Calculate the range of the array: data = [10, 20, 30, 40, 50].
  4. Calculate the 25th percentile of the array: data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].
  5. Calculate the correlation coefficient between two arrays: a = [1, 2, 3, 4, 5] and b = [2, 4, 6, 8, 10].

Reshaping Arrays

  1. Reshape a 1D array of 12 elements to a 3x4 array.
  2. Reshape a 2D array of shape (6, 2) to (3, 4).
  3. Create a 4x4 array and flatten it.
  4. Reshape a 3D array of shape (2, 3, 4) to (4, 3, 2).
  5. Reshape a 1D array to a 2D array with 1 row and many columns.

Element-wise Operations

  1. Create two arrays: a = [1, 3, 5] and b = [2, 4, 6]. Perform element-wise division.
  2. Create two arrays: a = [2, 4, 6] and b = [1, 3, 5]. Perform element-wise modulus.
  3. Add 10 to each element in the array: a = [1, 2, 3, 4, 5].
  4. Subtract 5 from each element in the array: b = [10, 20, 30, 40, 50].
  5. Square each element in the array: c = [1, 2, 3, 4, 5].

Slicing and Indexing

  1. Create a 3D array: array_3d = [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]. Access the element at [1, 1, 1].
  2. Slice the first layer of a 3D array: array_3d = [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]].
  3. Access all elements in the last row of the array: array_2d = [[10, 20, 30], [40, 50, 60], [70, 80, 90]].
  4. Access all elements in the first column of the array: array_2d = [[10, 20, 30], [40, 50, 60], [70, 80, 90]].
  5. Slice the last two rows and columns of the array: array_2d = [[10, 20, 30], [40, 50, 60], [70, 80, 90]].

Creating Random Arrays

  1. Generate a random array of 20 elements with values between 0 and 1.
  2. Create a 4x4 array with random integers between 10 and 20.
  3. Create a 3x3 array with random floats between -1 and 1.
  4. Generate an array of 15 random integers between 100 and 200.
  5. Create a 5x5 array with random floats between 0 and 10.

Stacking and Concatenating Arrays

  1. Vertically stack two arrays: a = [[1, 2], [3, 4]] and b = [[5, 6], [7, 8]].
  2. Horizontally stack two arrays: a = [[1, 2], [3, 4]] and b = [[5, 6], [7, 8]].
  3. Concatenate two 1D arrays: a = [1, 2, 3] and b = [4, 5, 6].
  4. Concatenate two 3x3 arrays along rows: a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] and b = [[10, 11, 12], [13, 14, 15], [16, 17, 18]].
  5. Concatenate two 3x3 arrays along columns: a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] and b = [[10, 11, 12], [13, 14, 15], [16, 17, 18]].