NumPy Assignment– 7

Advanced NumPy Concepts

Basic Questions

  1. Create a structured array with fields ‘name’ (string), ‘age’ (int), and ‘marks’ (float) for 3 students; print the array and access only the ‘name’ field.
  2. Create a record array with fields ‘id’ and ‘score’; print the first row and access ‘score’ column using attribute access.
  3. Create a masked array from [10, 20, -999, 30] where -999 is treated as missing; print the masked array and its filled version with 0.
  4. Apply a mask to an array [1, 2, 3, 4, 5] where values greater than 3 are hidden; print the masked and unmasked values.
  5. Create an array [5,3,8,6,7]; sort it with np.sort() and also print the indices returned by np.argsort().
  6. Sort a 2D array row-wise and column-wise using np.sort() with axis argument.
  7. Create an array [10,20,30,40,50]; use np.where() to find indices of elements greater than 25.
  8. Use np.nonzero() to find indices of non-zero elements from [0,1,0,2,3,0].
  9. Create a sorted array [10,20,30,40,50]; use np.searchsorted() to find where 35 and 10 should be inserted.
  10. Create an array [1,2,3,2,1,4,5]; find unique values with np.unique().
  11. Find the union of [1,2,3] and [3,4,5] using np.union1d().
  12. Find the intersection of [1,2,3,4] and [3,4,5,6] using np.intersect1d().
  13. Find the difference of [1,2,3,4] and [3,4,5,6] using np.setdiff1d().
  14. Create a 3×3 array with values 1–9 in row-major order and print its flattened version with order=’C’.
  15. Print the same array flattened with order=’F’ (column-major); compare outputs.
  16. Create two arrays of shape (3,1) and (1,3); add them using broadcasting and print the 3×3 result.
  17. Demonstrate broadcasting by subtracting the mean of each column from a 3×3 array.
  18. Optimize a row sum: compute sums of a 2D array by broadcasting instead of using a Python loop.
  19. Show memory size difference of a Python list vs NumPy array of 1 million integers using __sizeof__() and .nbytes.
  20. Verify contiguity of an array by printing .flags[‘C_CONTIGUOUS’] and .flags[‘F_CONTIGUOUS’].

Intermediate Questions

  1. Create a structured array with employees’ ‘id’, ‘name’, and ‘salary’; sort the array by ‘salary’.
  2. Create a record array with ‘x’ and ‘y’ coordinates; compute Euclidean distance for each record.
  3. Create a masked array with random integers 1–20, masking all even numbers; print unmasked odd values.
  4. Generate a masked array from a 4×4 random array by masking values greater than 50.
  5. Sort a 2D random integer array along axis 0 and axis 1 separately; compare results.
  6. Use np.argsort() on [40,10,20,30] to get sorting indices and use them to manually rearrange the array.
  7. Generate 20 random integers (1–100). Use np.where() to find all indices divisible by 7.
  8. Use np.searchsorted() to insert multiple values [15,55,75] into [10,20,30,40,50,60]; print the insertion indices.
  9. Use np.nonzero() to get positions of all odd numbers in an array [2,4,6,7,9,12,15].
  10. Create two arrays with duplicates. Use np.union1d() and np.intersect1d() to get unique union and intersection.
  11. Use np.setdiff1d() to find students present in class A [1,2,3,4] but not in class B [3,4,5].
  12. Generate a (3,3) array and flatten it using both ‘C’ and ‘F’ order. Compare memory layouts.
  13. Reshape a (4,6) array into (6,4) using order=’F’. Print both arrays to show layout differences.
  14. Compute row-wise mean of a (5,5) matrix using broadcasting instead of a loop.
  15. Normalize each column of a (4,4) random array by broadcasting division with column max.
  16. Use broadcasting to compute pairwise absolute differences of array [1,2,3,4] with itself.
  17. Create a (1000,1000) random array. Compare performance of np.sum() vs manual Python loops.
  18. Compare memory and speed difference between a C-ordered and F-ordered (1000,1000) array when transposed.
  19. Demonstrate how np.argsort() and np.take_along_axis() can be combined to sort rows of a 2D array.
  20. Implement z-score normalization of a (10,5) random dataset using broadcasting.

Advanced Questions

  1. Create a structured array for products (‘id’, ‘name’, ‘price’, ‘stock’). Sort by ‘price’ and then filter with stock < 50.
  2. Build a record array for students with fields ‘roll’, ‘marks1’, ‘marks2’, ‘marks3’; compute total marks and sort by it.
  3. Create a masked array for rainfall data [0,20,0,35,0,50] where 0 means missing; compute mean rainfall ignoring masked values.
  4. Create a 5×5 random matrix. Mask diagonal elements and compute mean of remaining values.
  5. Implement quicksort manually using np.argsort() on a random array of size 15.
  6. From a sorted array [10,20,30,40,50], insert values [5,25,60] at correct positions using np.searchsorted() and np.insert().
  7. Perform set operations on large arrays (size 1000) to find unique, intersection, and difference; measure performance.
  8. Compare memory layout performance: time arr.sum(axis=0) on (2000,2000) C-ordered vs F-ordered arrays.
  9. Optimize computation of all pairwise distances between 100 2D points using broadcasting instead of loops.
  10. Demonstrate advanced broadcasting trick: center a (1000,5) dataset by subtracting column means and then scale by column std—all in one line without loops.